我正在尝试制作一个绘制 9 个框的简单程序,当单击其中一个框时,会在其上绘制“X”。所以我画了 9 个盒子,然后我创建了一个类 ClickableLable。这个类继承了 QLabel 并重新实现了 mousePressEvent 函数,这样我就可以为每个点击的标签添加一个图像。
问题是,当我编译时,应用程序崩溃并且 Qt Creator 给了我一个“警告”: QObject::setParent: Cannot set parent, new parent is in a different thread
我没有使用任何线程/进程或类似的东西。我已经在互联网上搜索了这个错误,但我没有找到任何解决方案。
可点击标签.h
#ifndef CLICKABLELABEL_H
#define CLICKABLELABEL_H
#include <QLabel>
#include <QPixmap>
#include <QMouseEvent>
class ClickableLabel : public QLabel
{
Q_OBJECT
public:
ClickableLabel(QWidget *parent = 0);
protected:
void mousePressEvent(QMouseEvent *ev);
private:
QPixmap *zero;
QPixmap *x;
};
#endif // CLICKABLELABEL_H
ClickableLabel.cpp
#include "clickablelabel.h"
ClickableLabel::ClickableLabel(QWidget *parent) :
QLabel(parent)
{;
x = new QPixmap("x.png");
}
void ClickableLabel::mousePressEvent(QMouseEvent *ev){
if (ev->button() == Qt::LeftButton) {
this->setPixmap(*x);
}
}
主窗口.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "clickablelabel.h"
#include <QtGui>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
void set_up_widgets();
protected:
void paintEvent(QPaintEvent *event);
private:
ClickableLabel *lab1;
ClickableLabel *lab2;
ClickableLabel *lab3;
ClickableLabel *lab4;
ClickableLabel *lab5;
ClickableLabel *lab6;
ClickableLabel *lab7;
ClickableLabel *lab8;
ClickableLabel *lab9;
QHBoxLayout *lay1;
QHBoxLayout *lay2;
QHBoxLayout *lay3;
QVBoxLayout *layout;
};
#endif // MAINWINDOW_H
ClickableLabel.cpp
#include "clickablelabel.h"
ClickableLabel::ClickableLabel(QWidget *parent) :
QLabel(parent)
{;
x = new QPixmap("x.png");
}
void ClickableLabel::mousePressEvent(QMouseEvent *ev){
if (ev->button() == Qt::LeftButton) {
this->setPixmap(*x);
}
}
主文件
#include "mainwindow.h"
void center(QWidget &widget){
QDesktopWidget *desktop = QApplication::desktop();
int x = 800;
int y = 600;
int sWidth = (desktop->width() - 800) / 2;
int sHeight = (desktop->height() - 600) / 2;
widget.setGeometry(sWidth, sHeight, x, y);
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
center(w);
w.show();
return a.exec();
}
主窗口.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
this->setWindowTitle("T");
this->setToolTip("T");
set_up_widgets();
}
void MainWindow::paintEvent(QPaintEvent *event){
QPainter painter(this);
QPen pen(Qt::blue, 7, Qt::SolidLine, Qt::RoundCap);
int x = this->width() - 14;
int y = this->height() - 14;
int thirth_x = x / 3;
int thirth_y = y / 3;
painter.setPen(pen);
painter.drawLine(thirth_x, 0, thirth_x, y);
painter.drawLine(thirth_x * 2 + 14, 0, thirth_x * 2 + 14, y);
painter.drawLine(0, thirth_y, x, thirth_y);
painter.drawLine(0, thirth_y * 2 + 14, x, thirth_y * 2 + 14);
}
void MainWindow::set_up_widgets(){
lab1 = new ClickableLabel(this);
lab2 = new ClickableLabel(this);
lab3 = new ClickableLabel(this);
lab4 = new ClickableLabel(this);
lab5 = new ClickableLabel(this);
lab6 = new ClickableLabel(this);
lab7 = new ClickableLabel(this);
lab8 = new ClickableLabel(this);
lab9 = new ClickableLabel(this);
lay1 = new QHBoxLayout();
lay2 = new QHBoxLayout();
lay3 = new QHBoxLayout();
lay1->addWidget(lab1);
lay1->addWidget(lab2);
lay1->addWidget(lab3);
lay2->addWidget(lab4);
lay2->addWidget(lab5);
lay2->addWidget(lab6);
lay3->addWidget(lab7);
lay3->addWidget(lab8);
lay3->addWidget(lab9);
layout->addLayout(lay1);
layout->addLayout(lay2);
layout->addLayout(lay3);
setLayout(layout);
}