金色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
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

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

报告相同问题?

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集