简体   繁体   English

QT-从成员QWidget连接错误的按钮

[英]QT - connecting wrong button from member QWidget

I have a main window which opens a new window and connects a button from the new window to a "close" function. 我有一个主窗口,它将打开一个新窗口,并将新窗口中的按钮连接到“关闭”功能。 The problem arises when that new window has more than one button; 当该新窗口具有多个按钮时,就会出现问题。 it will always connect the last created button instead of the explicited one. 它将始终连接最后创建的按钮,而不是显式按钮。 Here is a sample working code: 这是一个示例工作代码:

"main.cpp" “ main.cpp”

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}

"mainwindow.h" “ mainwindow.h”

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "screen_char_info.h"
#include <QMainWindow>

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    QPushButton *button_show_char_info;
    Screen_Char_Info *screen_char_info;

    QWidget *mainwidget;
    QVBoxLayout *layout_main;

    MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
        button_show_char_info = new QPushButton("Character info", this);
        layout_main = new QVBoxLayout();
        mainwidget = new QWidget(this);


        screen_char_info = NULL;

        QObject::connect (button_show_char_info, &QPushButton::clicked, [this]{
            if (screen_char_info == NULL) {
                screen_char_info = new Screen_Char_Info();
                screen_char_info->show();
                QObject::connect (screen_char_info->button_return, &QPushButton::clicked, [=] {
                    screen_char_info->close();
                    screen_char_info = NULL;
                });
            }
        });



        layout_main->addWidget(button_show_char_info);
        mainwidget->setLayout(layout_main);
        setCentralWidget(mainwidget);
    }

    ~MainWindow()
    {

    }

};

#endif // MAINWINDOW_H

"screen_char_info.h" “ screen_char_info.h”

#ifndef SCREEN_CHAR_INFO_H
#define SCREEN_CHAR_INFO_H

#include <QString>
#include <QMenu>
#include <QMenuBar>
#include <QLabel>
#include <QTextEdit>
#include <QPushButton>
#include <QWidget>
#include <QLineEdit>
#include <QGridLayout>

class Screen_Char_Info : public QWidget {
    Q_OBJECT

public:
    QPushButton *buttons_modify_attributes[15];
    QPushButton *button_return;
    QGridLayout *layout;

    Screen_Char_Info () {
        this->setAttribute(Qt::WA_DeleteOnClose);
        this->setWindowTitle("Character Info");
        layout = new QGridLayout(this);

        for (int i = 0; i <= 15; i++) {
            buttons_modify_attributes[i] = new QPushButton((i%2 ? "-" : "+"), this);
            connect(buttons_modify_attributes[i], &QPushButton::clicked, [this] {

            });
            layout->addWidget(buttons_modify_attributes[i], (i / 2), (i % 2), 1, 1);
        }


        layout->addWidget(button_return = new QPushButton("Return", this), 8, 0, 1, 1);


        this->setLayout(layout);
    }

};

#endif // SCREEN_CHAR_INFO_H

However, if i put the line layout->addWidget(button_return... before the for loop, the button that closes the window is the last "-" button, and not the return one. 但是,如果我在for循环之前放一行layout->addWidget(button_return... ,则关闭窗口的按钮是最后一个“-”按钮,而不是返回按钮。

The way you do the connect does not appear to be conventional. 您进行连接的方式似乎并不常见。 Try using traditional Qt way: 尝试使用传统的Qt方法:

connect(pButtonToPress, SIGNAL(clicked()), pObjectToHandle, SLOT(onClicked));

Provided QPushButton* pButtonPress actually pointing to QPushButton and pObjectToHandle to some object (can be 'this' pointer): 提供的QPushButton * pButtonPress实际上指向QPushButton,而pObjectToHandle指向某个对象(可以是'this'指针):

class ObjHandler
{
   public slot:
      void onClicked();
};

... should satisfy. ...应该满足。 SIGNAL and SLOT are macro that work with some help of Qt meta object compiler. SIGNAL和SLOT是可以在Qt元对象编译器的帮助下工作的宏。 That is why having slot: statement is highly critical. 这就是为什么使用slot:语句非常关键的原因。

Found the bug, I was declaring a button matrix with 15 elements, but iterating over a 16 element loop. 找到了这个错误,我在声明一个包含15个元素的按钮矩阵,但是迭代了16个元素的循环。 The 16th element was the return button, and was overwritten in the loop. 第16个元素是返回按钮,并在循环中被覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM