QT扩展主窗口到其他类或不同的方式

QT extend MainWindow to other class or diffrent way

本文关键字:方式 其他 扩展 窗口 QT      更新时间:2023-10-16

我有一个class printrectangle

class PrintRectangle : public QWidget
{
    Q_OBJECT
public:
    explicit PrintRectangle(QWidget *parent = 0);
private:
    void resetClickedIndex();
    void updateIndexFromPoint( const QPoint& point);
public:
    int mXIndex;
    int mYIndex;
    QVector<QPoint> points;
    bool clicked[5][5] = {};
    teacher tech;
    perceptron p[5][5];
    double techconst = 0.1;
signals:
public slots:
protected:
    void paintEvent(QPaintEvent *);
    void mousePressEvent(QMouseEvent *eventPress);
};

MainWindow

namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
private slots:
    void on_learn_clicked();
    void on_classify_clicked();
private:
    Ui::MainWindow *ui;
};

当我点击按钮时,我调用on_learn_clicked()函数。我想将clicked[5][5]数组转换为on_learn_clicked,因为当用户单击按钮时,我将此数组发送给其他对象。如何做到这一点?

目前还不清楚MainWindow和PrintRectangle小部件之间究竟是什么关系。我想按钮信号和PrintRectangle插槽是在MainWindow实现的某个地方连接的。

解决这个问题的一种方法是使用QSignalMapper,如@Noidea所述。

另一种方法是在连接时使用lambda作为插槽。通过这种方式,您可以捕获范围内的发送方/接收方或其他对象,并使用它们的成员。

您可以在新信号槽语法

中找到有关连接语法的一些信息

但是基本上你可以这样写:

connect(button, &QPushButton::clicked, this, [this, printRectangle]()
{
    // do smth with clicked[5][5] from printRectangle or just
    // retrieve it and call another method like:
    // this->processClicked(printRectangle->clicked);
    // or pass it to another object
}

这样,您可以将on_classify_clicked slot修改为带有bool[5][5]参数的常规方法来执行处理。