金色12点丶 2014-12-27 13:31 采纳率: 0%
浏览 1279

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;
}
}

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:40
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。