2017-05-29 30 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