我正在尝试将按钮单击连接到一个函数,并传递一个int值,但不断收到错误:没有匹配函数来调用Main :: connect ...我猜我不是初始化正确的东西?
mainwindow.cpp
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
setupUI();
}
MainWindow::~MainWindow() {
}
void MainWindow::setupUI() {
QFrame* frame = new QFrame(this);
_layout = new QVBoxLayout;
frame->setLayout(_layout);
parseXML();
QScrollArea* scrollArea = new QScrollArea;
scrollArea->setWidget(frame);
scrollArea->setWidgetResizable(true);
setCentralWidget(scrollArea);
}
void MainWindow::parseXML() {
this->parseItem(xml, count)
}
QMap<QString, QString> MainWindow::parseItem(QXmlStreamReader& xml, int count) {
QString valueName = "buttonid";
QSignalMapper *signalMapper = new QSignalMapper(this);
QPushButton* button = new QPushButton(valueName);
_layout->addWidget(button);
connect(signalMapper, SIGNAL(mapped(int)), this, SLOT(addMenu(int)));
signalMapper->setMapping(button, valueName);
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
return something;
}
void MainWindow::addMenu(int count) {
_layoutToAdd = new QVBoxLayout;
QPushButton* button = new QPushButton("New Button");
_layoutToAdd->addWidget(button);
_layout->insertLayout(count, _layoutToAdd, 0);
}
mainwindow.h
ublic:
MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void parseXML();
void addMenu(int count);
signals:
void clicked();
答案 0 :(得分:3)
您的代码中存在语法错误。计算括号:
connect(button, SIGNAL(clicked())), signalMapper, SIGNAL(map());
// ^ ^ ^^^^
// 1 2 3321 <-- All your parenthesis are closed at this point
// meaning you are calling connect() with only 2 parameters
答案 1 :(得分:1)
您必须在课堂描述中使用白色Q_OBJECT(用于使用信号和插槽)
class MainWindow: public QObject
{
Q_OBJECT