Qt 没有匹配的调用函数

发布于 2024-10-02 22:07:24 字数 1598 浏览 3 评论 0原文

我正在尝试将按钮单击连接到函数,并传递 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();

I'm trying to connect a button click to a function, and pass an int value, but keep getting an error: no matching function for call to Main::connect... I'm guessing that I'm not initializing something correct?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜兔 2024-10-09 22:07:24

您的代码中存在语法错误。数一下你的括号:

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

There's a syntax error in your code. Count your parenthesis :

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
静水深流 2024-10-09 22:07:24

您必须在类的描述中使用白色 Q_OBJECT (用于使用信号和槽)

class MainWindow: public QObject
{
  Q_OBJECT

You must white Q_OBJECT (for using signals and slots ) in descriction of your class

class MainWindow: public QObject
{
  Q_OBJECT
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文