问题描述
- QT paintEvent(QPaintEvent *)画图问题 急急急
- #include ""widget.h""
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(600600);
QLabel *label = new QLabel(tr(""Images side is 50 !""));
label->setFixedHeight(50);
QLineEdit *edit = new QLineEdit(tr(""50""));//QLineEdit *edit2 = new QLineEdit();QPushButton *button1 = new QPushButton(tr(""Random""));button1->setFixedHeight(50);QPushButton *button2 = new QPushButton(tr(""Previous""));button2->setFixedHeight(50);QPushButton *button3 = new QPushButton(tr(""Next""));button3->setFixedHeight(50);QGroupBox *box = new QGroupBox;QRadioButton *radio1 = new QRadioButton(tr(""3*3""));QRadioButton *radio2 = new QRadioButton(tr(""5*5""));QVBoxLayout *vbox = new QVBoxLayout;vbox->addWidget(radio1);vbox->addWidget(radio2);box->setLayout(vbox);QHBoxLayout *layout = new QHBoxLayout;layout->addWidget(label);layout->addWidget(edit);layout->addWidget(button1);//layout->addWidget(edit2);layout->addWidget(button2);layout->addWidget(button3);layout->addWidget(box);layout->setAlignment(Qt::AlignTop);setLayout(layout);L=edit->text().toInt();//**这里**//QString str = QString::number(L);//edit2->setText(str);connect(button1SIGNAL(clicked())thisSLOT(change()));
}
void Widget::change()
{
//this->repaint();
this->update();
}void Widget::random()
{
int m = qrand()%100;
if(m<=20){
color = false;
}else{
color = true;
}}
void Widget::paintEvent(QPaintEvent )
{
int length;
int i=0;
int j=70;//*这里**
//length=edit->text().toInt();//为什么用这两行行代码程序就崩溃了,要怎样才能实现根据edit中输入的数值
//L=edit->text().toInt(); // 来刷新我生成矩形的数目啊
length=L;
QPainter painter(this);
for(int m=0;m<=length;m++){
for(int n=0;n<=length;n++){
painter.begin(this);
random();
if(color){
painter.setBrush(QBrush(Qt::whiteQt::SolidPattern));
}else{
painter.setBrush(QBrush(Qt::blackQt::SolidPattern));
}
painter.setPen(QPen(Qt::gray));
painter.drawRect(iji+10j+10);
painter.end();
i=i+10;
}
i=0;
j=j+10;
}
}