gpt4 book ai didi

c++ - 当我在运行程序之外的任何地方单击时,Qt 所有的绘图都消失了

转载 作者:行者123 更新时间:2023-11-28 01:30:32 29 4
gpt4 key购买 nike

我正在尝试在 Qt 上制作类似网络的东西,但是在运行程序时我遇到了一个奇怪的错误,当我单击正在运行的程序之外的任何地方或打开任何其他应用程序时,所有绘图都会消失。 this is a pic of the running program at first

and this when I click anywhere outside the running program

这是代码

#include "widget.h"
#include "ui_widget.h"


Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setFixedSize(210*6+10,this->height());

Lobes["Frontal Lobe"] = {"Higher Mental \n function",
"Broca's area",
"Motor Function\n(Eye Movement)"};

Lobes["Partial Lobe"] = {"Motor Function\n(Muscles)",
"Somatosensory \n association\narea",
"Sensory Area"};

Lobes["Temporal Lobe"] = {"Association area",
"Auditory area",
"Wernicke's area"};

Lobes["Occiptal Lobe"] = {"Visual area"};

SizeCalculations();
}

Widget::~Widget()
{
delete ui;
}
void Widget::paintEvent(QPaintEvent* e)
{

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::red);


it = Lobes.begin();
int space = 10;
for(int x = 0; x < 6; x++)
{

if(x < 6)
{
Node *Rec = new Node(CognitveSpace,50,CogitiveFunctions[x],0,szCogntive);

painter.drawRect(Rec->getRectangle());
painter.drawText(Rec->getRectangle(), Qt::AlignCenter, Rec->getName());
// painter.fillRect(Rec->getRectangle(),Qt::red);
CognitveSpace += szCogntive.width()+10;
}
if(x < 4)
{
Node *Rec = new Node(LobesSpace,250,it->first,0,szLobes);
painter.drawRect(Rec->getRectangle());
painter.drawText(Rec->getRectangle(), Qt::AlignCenter, Rec->getName());
// delete Rec;

for(size_t index = 0; index < it->second.size();index++)
{

Node *Rec = new Node(AreasSpace,450,it->second[index],0,szAreas);
painter.drawRect(Rec->getRectangle());
painter.drawText(Rec->getRectangle(), Qt::AlignCenter, Rec->getName());
space += 180;
//delete Rec;
AreasSpace += szAreas.width()+10;
}
it++;
LobesSpace += szLobes.width()+10;
AreasSpace = LobesSpace +15;
}

}

}


void Widget::SizeCalculations()
{
szCogntive.setWidth((this->width() / 6) - CognitveSpace);
szLobes.setWidth((this->width() / 4) - LobesSpace);
szAreas.setWidth(szLobes.width()/3 - 15);
}
void Widget::on_pushButton_clicked()
{

}

最佳答案

这是一个简单的问题:绘制事件实际上应该是常量。您在每次重绘时重复修改各种尺寸。但是我们可以让编译器帮你解决问题——让我们修改代码让你的对象在绘画中保持不变:

void Widget::paintEvent(QPaintEvent *event) {
QPainter painter(this);
paintEvent(event, painter);
}

void Widget::paintEvent(QPaintEvent *, QPainter &painter) const { // const is important!
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::red);
// etc - rest of the code follows
}

// Declaration in the class
void paintEvent(QPaintEvent *, QPainter &) const; // const!!

现在编译 - 编译器会将您修改对象的所有位置标记为错误。您需要将这些计算移到别处,以便绘画不会修改对象。然后它会起作用。

关于c++ - 当我在运行程序之外的任何地方单击时,Qt 所有的绘图都消失了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51733917/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com