2017-05-29 8 views
0

私は2つの.uiファイルはQtのデザイナを使用して作成したカスタムウィジェット

  1. main_window.ui - クラスQMainWindowのみ1 QTabWidget
  2. を含む(名前mywindowの)
  3. my_widget.ui - クラスQWidgetの(名前はMyWidget)を含みますQLabelとのQPushButton各

私はこの

のようなものを読み込む設定ファイルを持っています

ProcessAタブ1; ProcessB Tab2; ProcessC Tab1

上記から明らかなように、Tab1にはAとC、Tab2にはBが必要です。私は希望のタブを作成することができました。私は2番目のUIのインスタンスを作成し、タブを設定したいと思います。

私が代わりにはMyWidgetクラスののQPushButtonクラスを使用することができたし、(正しい)次の出力を持っている

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(tabWidgetという名前の単一QTabWidgetを含むだけでQMainWindow)

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

form.hpp(MyWidgetを表すはずです)。

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

フォームはQLabelとQVBoxLayoutでのQPushButtonが含まれています。UI:

<?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

私はconfigファイル内のすべてのプロセスに対して新しいインスタンスを作成します。私はその部分が正常に動作していることを知っている。私が探しているのは、ラベルとボタンを含む私のウィジェットを作成し、それをメインウィンドウに動的に追加する方法です。 –

+0

最小限の例が追加されました。それがあなたを助けないなら、あなたは働くものを追加しなければなりません。 – Blechdose

関連する問題