QT C++-如何引用另一个类中的小部件

QT C++ - How to refer to a widget in another class

本文关键字:另一个 小部 引用 C++- 何引用 QT      更新时间:2023-10-16

我正在尝试制作一个按钮,将小部件添加到布局中。

小部件在另一个类中,完全来自UI。

通常,我会制作一个指向另一个类(没有UI,有小部件)的指针,它将是InkSpot *ink(类名为InkSpot),然后我会说ui->paintAreaLayout->addWidget(ink->widget);,但那一行会导致应用程序崩溃,如果我删除它并添加一些不引用该类的东西,那么它就不会崩溃。

我将提供大部分代码,这样你就可以更好地看到我正在尝试做什么。测试按钮位于第一个代码块的底部,这就是有问题的代码块:

inkpupt.cpp

#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include "newdialog.h"
#include "inkspot.h"
#include <Qt>
#include <QtCore>
#include <QtGui>
#include <QtWidgets>
#include <QDialog>
#include <QMainWindow>

InkPuppet::InkPuppet(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::InkPuppet)
{
    ui->setupUi(this);
    //connect the frame range boxes to the timeslider
    connect(ui->lowerFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMinimum(int)));
    connect(ui->upperFrameBox, SIGNAL(valueChanged(int)), this, SLOT(setMaximum(int)));
    //connect the menu items
    connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(actionNew()));
    //connect test
    connect(ui->testButton, SIGNAL(clicked()), this, SLOT(testButton()));
}
InkPuppet::~InkPuppet()
{
    delete ui;
}
void InkPuppet::setMinimum(int value)
{
    ui->timeSlider->setMinimum(value);
}
void InkPuppet::setMaximum(int value)
{
    ui->timeSlider->setMaximum(value);
}
void InkPuppet::actionNew()
{
    NewDialog *dialog = new NewDialog;
    dialog->setModal(true);
    dialog->show();
}
void InkPuppet::testButton()
{
    ui->testButton->setText("working");
    //ui->paintAreaLayout->addWidget(ink->label);
    //qDebug() << ui->testButton;
    ui->paintAreaLayout->addWidget(ink->widget);
}

inkpuppet.h

#ifndef INKPUPPET_H
#define INKPUPPET_H
#include "inkspot.h"
#include <QMainWindow>
namespace Ui {
class InkPuppet;
}
class InkPuppet : public QMainWindow
{
    Q_OBJECT
public:
    explicit InkPuppet(QWidget *parent = 0);
    ~InkPuppet();
    InkSpot *ink;
private slots:
    void setMinimum(int value);
    void setMaximum(int value);
    void actionNew();
    void testButton();
public:
    Ui::InkPuppet *ui;
};
#endif // INKPUPPET_H

inkspot.cpp

#include "inkspot.h"
#include "inkpuppet.h"
#include "ui_inkpuppet.h"
#include <QtCore>
#include <QtGui>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>

void InkSpot::paintEvent(QPaintEvent *event)
{
    QWidget *widget = new QWidget();
    widget->setFixedSize(100, 150);
    QLabel *label = new QLabel();
    label->setText("test");
    widget->layout()->addWidget(label);
    //InkPuppet.ui->paintAreaLayout->addWidget(widget);
    QFile *brushInput; //takes raw 8 bit grayscale image, 8 bit values only
    char *brushProto;
    uchar *brushData;
    brushInput = new QFile("x:\Development\InkPuppet\brush.raw"); //open the raw file
    brushInput->open(QIODevice::ReadOnly);
    QDataStream in;
    in.setDevice(brushInput);
    int size = brushInput->size(); //set size to length of raw file
    brushProto = new char[size];
    in.readRawData(brushProto, size); //read file into prototype
    brushData = new uchar[size];
    for(int i = 0; i < size; ++i)
    {
        brushData[i] = (uchar)brushProto[i]; //copy char to uchar array
    }
    QImage test(brushData, 128, 128, QImage::Format_Indexed8);
    QImage test2(128, 128, QImage::Format_ARGB32);
    QVector<QRgb> vectorColors(256); //create color table
    for(int c = 0; c < 256; c++)
    {
        vectorColors[c] = qRgb(c, c, c);
    }
    test.setColorTable(vectorColors);
    for(int iX = 0; iX < 100; ++iX)
    {
        for(int iY = 0; iY < 100; ++iY)
        {
            test2.setPixel(iX, iY, qRgba(255 - (qrand() % 100), 0 + (qrand() % 100), 0 + (qrand() % 100), qAbs((int)test.pixel(iX, iY)-255)));
        }
    }
    //final conversion for stencil and color brush
    QPixmap testPixmap = QPixmap::fromImage(test2);
    QPixmap testPixmap2 = QPixmap::fromImage(test);
    QPainter painter(this);
    painter.drawPixmap(150, 50, 100, 100, testPixmap);
    painter.drawPixmap(50, 50, 100, 100, testPixmap2);
    delete[] brushProto;
    delete[] brushData;
    delete brushInput;
}

inkspot.h

#ifndef INKSPOT_H
#define INKSPOT_H
#include <QObject>
#include <QWidget>
#include <QPainter>
#include <QPaintEvent>
#include <QLabel>
namespace Ui {
class InkSpot;
}
class InkSpot : public QWidget
{
    Q_OBJECT
public:
    explicit InkSpot(QWidget *parent = 0);
    ~InkSpot();
    void draw(QPainter *painter);
    QWidget *widget;
    QLabel *label;
signals:
public slots:
protected:
    void paintEvent(QPaintEvent *event);
private:
    Ui::InkSpot *ui;
};
#endif // INKSPOT_H

我认为,您的错误在另一个地方。如果使用指针,请在每次使用时检查它们是否不等于NULL

我没有在您的代码中找到ink的初始化。请检查一下。

此外,不时地清理构建——这可以防止一些错误。

这可能很愚蠢,但如果您将包含小部件的标头添加到UI类中,该怎么办?或者(重新)定义UI中的小部件,以便使用它?

尝试将头(带有小部件的定义)添加到UI类中,这样UI就知道小部件了。定义是定义元素和函数的地方(放在标题中的东西。

或者将定义添加到UI的头文件中,如果您不需要头的其余元素,但第一个选项会更好。