QT paintEvent(QPaintEvent *)画图问题 急急急

#include "widget.h"

Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setFixedSize(600,600);
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(button1,SIGNAL(clicked()),this,SLOT(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::white,Qt::SolidPattern));
}else{
painter.setBrush(QBrush(Qt::black,Qt::SolidPattern));
}
painter.setPen(QPen(Qt::gray));
painter.drawRect(i,j,i+10,j+10);
painter.end();
i=i+10;
}
i=0;
j=j+10;
}
}

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐