我正在编写一个由以下组成的小实用程序:
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);
感谢您指出解决此问题的正确方向。