2012-10-05 108 views
0

我試圖在推送'pushButton_2'之後在MainWindow中的圖層上顯示小部件'widg'但我收到此錯誤:'期望的初級表達式'之前')令牌「錯誤:在''''令牌之前預期的初級表達式

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; 
} 

請幫助我,什麼是我的錯?

+1

哪一行導致錯誤? – Blender

+0

第22行,「ui-> verticalLayout_3-> addWidget(widg);」 – SerJS

+0

如何定義'ui'和'widg'?並且'ui'是否有一個叫'verticalLayout_3'的成員? –

回答

1

這是很難準確理解你的意圖是什麼,但也許你的意思是這樣的:

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

非常感謝!現在一切正常) – SerJS

相關問題