简体   繁体   English

Qt应用程序C ++的自定义小部件

[英]Custom widget for Qt Application C++

I have 2 .ui files created using Qt Designer 我有2个使用Qt Designer创建的.ui文件

  1. main_window.ui - class QMainWindow (name MyWindow) containing only 1 QTabWidget main_window.ui -QMainWindow类(名称为MyWindow),仅包含1个QTabWidget
  2. my_widget.ui - class QWidget (name MyWidget) containing a QLabel and QPushButton each my_widget.ui -QWidget(名称为MyWidget)类,每个类均包含QLabel和QPushButton

I have a configuration file which reads something like this 我有一个配置文件,内容如下:

ProcessA Tab1 ; ProcessA Tab1; ProcessB Tab2 ; ProcessB Tab2; ProcessC Tab1 ProcessC Tab1

As clear from above, I need A and C to be in Tab1 and B in Tab2. 从上面可以清楚地看到,我需要A和C分别位于Tab1和B中。 I have been able to create the desired tabs. 我已经能够创建所需的标签。 I want to create instances of the 2nd UI and populate the tabs. 我想创建第二个UI的实例并填充选项卡。

I have been able to use QPushButton class instead of the MyWidget Class and got the following output (which is correct) 我已经能够使用QPushButton类而不是MyWidget类,并获得以下输出(正确)

QPushButton输出

When I try to use the MyWidget Class, I end up getting blank tabs. 当我尝试使用MyWidget类时,最终会出现空白标签。

Overview of code - 代码概述-

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

The setupTabs function performs exactly as desired and thus I have mentioned the code very briefly. setupTabs函数的性能完全符合要求,因此我已经简短地提到了代码。

MyWidget.h looks like this MyWidget.h看起来像这样

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

Please let me know what I am doing wrong. 请让我知道我在做什么错。

It's a bit hard to tell from your pseudo code, but it seems like you're always adding the same widgets (MyWidget) to the layouts. 从伪代码很难分辨,但是似乎总是在向布局添加相同的小部件(MyWidget)。 Is your last tab containing the widgets, but the others don't? 您的最后一个标签中是否包含小部件,而其他标签中没有?

Best regards 最好的祝福


Minimal example: 最小示例:

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 (just a QMainWindow containing a single QTabWidget named tabWidget) mainwindow.cpp(只是一个QMainWindow,其中包含一个名为tabWidget的QTabWidget)

#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 (should represent your MyWidget). form.hpp(应代表您的MyWidget)。 It contains a QLabel and a QPushButton in a QVBoxLayout: 它在QVBoxLayout中包含一个QLabel和一个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: 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);
}

form.ui: form.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>

Hope that helps, 希望能有所帮助,

Best regards 最好的祝福


Second edit, same example as above, but form.ui has no layout on the Form (QWidget) anymore (looks at least like the problem in your description): 第二次编辑,与上面的示例相同,但是form.ui在Form(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>

Best regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM