2017-05-29 55 views
0

我有2個的.ui文件中使用Qt Designer創建定製插件Qt的應用C++

  1. main_window.ui - 類的QMainWindow僅含有1 QTabWidget
  2. (名稱爲mywindow)
  3. my_widget.ui - 包含類QWidget的(名稱進myWidget)一QLabel和QPushButton每個

我有一個配置文件,其內容是這樣的

ProcessA Tab1;

ProcessA Tab1; ProcessB Tab2; ProcessC Tab1

如上所述,我需要A和C在Tab1和B在Tab2。我已經能夠創建所需的選項卡。我想創建第二UI的實例並填充選項卡。

我已經能夠使用QPushButton類,而不是進myWidget類的,並得到下面的輸出(這是正確的)

QPushButton output

當我嘗試使用進myWidget類,我最終得到空白標籤。的代碼

概述 -

MyWindow::setupTabs(std::string fileName) 
{ 
    Read file 
    for each process in file: 
    MyWidget *temp = new MyWidget(); 
    Fill text in label and button 
    Create std::vector<std::vector<MyWidget*> > and fill according to tabs 
    for each tab call populateTab() 
} 

MyWindow::populateTab(processList, tabName) 
{ 
    QFrame* group = new QFrame(); 
    QVBoxLayout* vbox = new QVBoxLayout(); 
    vbox->setSpacing(0); 
    group->setLayout(vbox); 
    QScrollArea* scrollArea = new QScrollArea(); 
    for (size_t i = 0; i < processList.size(); ++i) 
    vbox->addWidget(processList[i]); //vbox->addWidget(new QPushButton); works fine 
    scrollArea->setWidget(group); 
    ui->tabWidget->addTab(scrollArea, tabName); 
} 

的setupTabs功能完全一樣期望執行,因此我也非常簡要地提到的代碼。

MyWidget.h看起來像這樣

namespace Ui { 
class MyWidget; 
} 
class MyWidget : public QWidget 
{ 
    Q_OBJECT 
public: 
    explicit MyWidget(QWidget *parent = 0); 
    ~MyWidget(); 
private: 
    Ui::MyWidget *ui; 
}; 

請讓我知道我做錯了。

回答

1

從您的僞代碼中分辨出來有點難,但看起來您總是將相同的小部件(MyWidget)添加到佈局中。您的最後一個選項卡是否包含小部件,但其他選項不包含?

此致


最少例如:

mainwindow.hpp

#ifndef MAINWINDOW_HPP 
#define MAINWINDOW_HPP 

#include <QMainWindow> 

namespace Ui { 
class MainWindow; 
} 

class MainWindow : public QMainWindow 
{ 
    Q_OBJECT 

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

private: 
    Ui::MainWindow *ui; 
}; 

#endif // MAINWINDOW_HPP 

mainwindow.cpp

#include "mainwindow.hpp" 
#include "ui_mainwindow.h" 
#include "form.hpp" 

MainWindow::MainWindow(QWidget *parent) : 
    QMainWindow(parent), 
    ui(new Ui::MainWindow) 
{ 
    ui->setupUi(this); 
    ui->tabWidget->clear(); // Clear the two Tabs that are shown by default 
    QStringList fileEntries(QStringList() << "widget1" << "widget2" << "widget3" << "widget4"); // A QStringList simulating the file entries 

    int count = 1; 
    foreach (QString entry, fileEntries) { // Loop through all the entries and add the custom widget on the go 
     Form *myWidget = new Form(this); 
     myWidget->set(entry + "Label", entry + "Button"); 
     ui->tabWidget->addTab(myWidget, "New Tab " + QString::number(count++)); 
    } 
} 

MainWindow::~MainWindow() 
{ 
    delete ui; 
} 
(只是一個含有名爲tabWidget單個QTabWidget的QMainWindow)

form.hpp(應該代表你的MyWidget)。它包含一個QLabel並在QVBoxLayout一個QPushButton:

#ifndef FORM_HPP 
#define FORM_HPP 

#include <QWidget> 

namespace Ui { 
class Form; 
} 

class Form : public QWidget 
{ 
    Q_OBJECT 

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

    void set(QString const&labelText, QString const&buttonText); 

    private: 
     Ui::Form *ui; 
    }; 

    #endif // FORM_HPP 

form.cpp:

#include "form.hpp" 
#include "ui_form.h" 

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

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

void Form::set(const QString &labelText, const QString &buttonText) 
{ 
    ui->label->setText(labelText); 
    ui->pushButton->setText(buttonText); 
} 

形式。用戶界面:

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Form</class> 
<widget class="QWidget" name="Form"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>400</width> 
    <height>300</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Form</string> 
    </property> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QLabel" name="label"> 
    <property name="text"> 
     <string>TextLabel</string> 
    </property> 
    </widget> 
    </item> 
    <item> 
    <widget class="QPushButton" name="pushButton"> 
    <property name="text"> 
     <string>PushButton</string> 
    </property> 
    </widget> 
    </item> 
    </layout> 
</widget> 
<resources/> 
<connections/> 
</ui> 

希望幫助,

問候


其次編輯,如上面同樣的例子,但form.ui的形式(QWidget的)上沒有佈局了(長相至少像你描述的問題):

<?xml version="1.0" encoding="UTF-8"?> 
<ui version="4.0"> 
<class>Form</class> 
<widget class="QWidget" name="Form"> 
    <property name="geometry"> 
    <rect> 
    <x>0</x> 
    <y>0</y> 
    <width>737</width> 
    <height>523</height> 
    </rect> 
    </property> 
    <property name="windowTitle"> 
    <string>Form</string> 
    </property> 
    <widget class="QWidget" name="verticalLayoutWidget"> 
    <property name="geometry"> 
    <rect> 
    <x>480</x> 
    <y>350</y> 
    <width>160</width> 
    <height>80</height> 
    </rect> 
    </property> 
    <layout class="QVBoxLayout" name="verticalLayout"> 
    <item> 
    <widget class="QLabel" name="label"> 
     <property name="text"> 
     <string>TextLabel</string> 
     </property> 
    </widget> 
    </item> 
    <item> 
    <widget class="QPushButton" name="pushButton"> 
     <property name="text"> 
     <string>PushButton</string> 
     </property> 
    </widget> 
    </item> 
    </layout> 
    </widget> 
</widget> 
<resources/> 
<connections/> 
</ui> 

最好的問候

+0

沒有任何選項卡包含「MyWidget」。但是,當我添加QPushButton代替那個時,輸出如預期。每個選項卡都有適當的按鈕。 –

+0

我爲配置文件中的每個進程創建一個新實例。我知道那部分工作正常。我正在尋找的是一種創建我自己的小部件的方法,它包含一個標籤和一個按鈕,並將其動態添加到主窗口, –

+0

添加了一個簡單示例。如果這對你沒有幫助,你必須添加一個工作。 – Blechdose