我正在尝试在推送'pushButton_2'后在MainWindow上的图层中显示小部件'widg',但我收到此错误:“预期的' - '之前的'primary'表达式”
mainwindow.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);
}
mainwindow.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;
}
请帮助我,我的错误是什么?
答案 0 :(得分:1)
很难准确理解你的意图是什么,但也许你的意思是:
void MainWindow::slotPush2()
{
ui->verticalLayout_3->addWidget(new widg(this));
}