【问题标题】:problem with SIGNAL SLOT new notation for QAbstractButtonQAbstractButton 的 SIGNAL SLOT 新符号的问题
【发布时间】:2020-10-14 18:39:19
【问题描述】:

我正在编写一个由 :

组成的小实用程序

1) 4 QToolBar

2) 1 QToolBox

3) 1 QMenu

4) 1 QGraphicsView

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

我每天都使用新的表示法,并且在我不得不使用“抽象”实体(例如 QAbstractButton 方法)之前从未遇到任何问题。

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

ma​​inwindow.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;

ma​​inwindow.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&lt;&gt; 尝试将其转换为QAbstractButton,如下所示:

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

2)我也根据official documentation尝试了以下方法,但也没有用:

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);

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

【问题讨论】:

  • @eyllanesc,感谢您停下来阅读这个问题。这是下面用户的建议,但这并没有解决问题。我遇到了一个奇怪的错误:backgroundButtonClicked is not a member of QAbstractButton

标签: c++ c++11 qt5 qbuttongroup qabstractbutton


【解决方案1】:

您有以下错误:

  • 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);

我建议你检查:

【讨论】:

  • 好的,谢谢最后一个工作!感谢您指向特定的overload for the abstract entity。我还看到 @QAbstractButton::backgroundButtonClicked 但实际上是 @MainWindow::backgroundButtonClicked :)
  • @Emanuele 可能值得您尝试QButtonGroup::buttonToggled 而不是buttonClicked,因为您使用的是选中按钮
猜你喜欢
  • 2011-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多