c++ - 错误 : expected primary-expression before ')' token

标签 c++ qt

我尝试在按下“pushButton_2”后在 MainWindow 的层中显示小部件“widg”,但我收到此错误:“expected primary-expression before ')' token”

主窗口.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "widg.h"
#include "ui_widg.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QObject::connect(ui->pushButton_2, SIGNAL(clicked()), SLOT(slotPush2()));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::slotPush2()
{
    ui->verticalLayout_3->addWidget(widg);
}

主窗口.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

private slots:
    void slotPush2();
};


#endif // MAINWINDOW_H

widg.h

#ifndef WIDG_H
#define WIDG_H

#include <QWidget>

namespace Ui {
class widg;
}

class widg : public QWidget
{
    Q_OBJECT

public:
    explicit widg(QWidget *parent = 0);
    ~widg();

private:
    Ui::widg *ui;
};

#endif // WIDG_H

widg.cpp

#include "widg.h"
#include "ui_widg.h"

widg::widg(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widg)
{
    ui->setupUi(this);
}

widg::~widg()
{
    delete ui;
}

请帮帮我,我的错误是什么?

最佳答案

很难准确理解您的意图,但也许您的意思是:

void MainWindow::slotPush2()
{
    ui->verticalLayout_3->addWidget(new widg(this));
}

关于c++ - 错误 : expected primary-expression before ')' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12755231/

相关文章:

c++ - Visual Studio 2019 测试资源管理器没有找到 c++ google 测试

c++ - 递归代码的运行时错误

c++ - 是否有可用的*免费* QT UI 测试框架?

c++ - 如何使用颜色将 QPlainTextEdit 内容渲染到 QPixmap?

Qt@5.5 有效,但 Qt 无效

c++ - 二进制字符串: unexpected end in precompiled chunk

c++ - '_IsFirstIteration':std::lower_bound 中未引用的形参

c++ - 在 C++ 中使用带有正则表达式的 g++ 和 Visual Studio 14 2015 编译器时的不同结果

Qt 文化感知的 QString 容器类

qt - 如何从 QT 中的 RTSP 流解析 ONVIF 元数据?