1

我正在编写一个由以下组成的小实用程序:

1) 4QToolBar

2) 1QToolBox

3) 1QMenu

4) 1QGraphicsView

我有一些按钮QToolBox,每个按钮代表一个具有不同间距的网格。每次用户单击QToolBox the spacing on the QGraphicsScene 上的按钮时都会发生变化。

我每天都使用新的符号并且从来没有遇到任何问题,直到我不得不使用诸如QAbstractButton方法之类的“抽象”实体。

我遇到的问题是我似乎没有正确设置连接信号/插槽QAbstractButton

主窗口.h

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void backgroundButtonClicked(QAbstractButton *button);

private:
    void createToolBox();
    QButtonGroup *buttonGroup;
    QButtonGroup *backgroundButtonGroup;

主窗口.cpp

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    createToolBox();

    scene = new DiagramScene(itemMenu,this);
    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(toolBox);
    view = new QGraphicsView(scene);
    QWidget *widget = new QWidget;
    widget->setLayout(layout);
    setCentralWidget(widget);
}

void MainWindow::backgroundButtonClicked(QAbstractButton *button)
{
    QList<QAbstractButton*> buttons = backgroundButtonGroup->buttons();
    foreach (QAbstractButton *myButton, buttons) {
        if(myButton != button)
            button->setChecked(false);
    }
    QString text = button->text();
    if(text == tr("Blue Grid"))
        scene->setBackgroundBrush(QPixmap(":/images/background1.png"));
    else if(text == tr("White Grid"))
        scene->setBackgroundBrush(QPixmap(":/images/background2.png"));
    else if(text == tr("Gray Grid"))
        scene->setBackgroundBrush(QPixmap(":/images/background3.png"));
    else
        scene->setBackgroundBrush(QPixmap(":/images/background4.png"));

    scene->update();
    view->update();
}


void MainWindow::createToolBox()
{
    buttonGroup = new QButtonGroup(this);
    buttonGroup->setExclusive(false);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(createCellWidget(tr("Conditional"), DiagramItem::Conditional), 0, 0);
    layout->addWidget(createCellWidget(tr("Process"), DiagramItem::Step), 0, 1);
    layout->addWidget(createCellWidget(tr("Input/Output"), DiagramItem::Io), 1, 0);

    QToolButton *textButton = new QToolButton;
    textButton->setCheckable(true);
    buttonGroup->addButton(textButton, InsertTextButton);
    textButton->setIcon(QIcon(QPixmap(":/images/textpointer.png")));
    textButton->setIconSize(QSize(50, 50));
    
    QWidget *itemWidget = new QWidget;
    itemWidget->setLayout(layout);

    backgroundButtonGroup = new QButtonGroup(this);

    // This is my first attempt but it does not work:

    QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked());


}

我尝试过的不同选项是:

1)通过static_cast<>尝试将其转换QAbstractButton为如下所示:

QObject::connect(static_cast<QAbstractButton*>(backgroundButtonGroup), this, static_cast<QAbstractButton>(backgroundButtonClicked(void)));

2)我也根据官方文档尝试了以下方法,但也没有用:

QObject::connect(backgroundButtonGroup, &QAbstractButton::clicked, this, &QAbstractButton::backgroundButtonClicked);

除此之外,我偏离了下面的以下结构(前往Q_INVOKE),但我担心我过于复杂化了这个问题,但只是想包括我试图探索的额外解决方案:

const bool connected = connect(sender, &Sender::aSignal, 
                               receiver, &Receiver::aSlot);
Q_ASSERT(connected);
Q_UNUSED(connected);

感谢您指出解决此问题的正确方向。

4

1 回答 1

5

您有以下错误:

  • QButtonGroup 没有clicked信号但是buttonClicked
  • 您不得使用().

一般的语法是:obj,& Class_of_obj,在你的情况下,backgroundButtonGroup 是 QButtonGroup,这是 MainWindow,所以初步的语法是:

QObject::connect(backgroundButtonGroup, &QButtonGroup::buttonClicked, this, &MainWindow::backgroundButtonClicked);
  • 但是buttonClicked 是重载的,那么您必须使用QOverload 来指示签名。

考虑到上述情况,解决方案是:

QObject::connect(backgroundButtonGroup, QOverload<QAbstractButton *>::of(&QButtonGroup::buttonClicked), this, &MainWindow::backgroundButtonClicked);

我建议你检查:

于 2020-10-14T19:02:12.790 回答