在我的mainwidow.cpp构造函数中,我这样做:

TLDAlgoSettings = new TLDSettings(ui->algoSettingsStackedWidget);

ui->algoSettingsStackedWidget->addWidget(TLDAlgoSettings);
ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);

TLDSettings是QWidget:
#ifndef TLDSETTINGS_H
#define TLDSETTINGS_H

#include <QWidget>

namespace Ui {
class TLDSettings;
}

class TLDSettings : public QWidget
{
    Q_OBJECT

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

private:
    Ui::TLDSettings *ui;
};

#endif // TLDSETTINGS_H

很简单;但是,由于它无法正常工作,我似乎缺少了一些东西:执行ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);时,我的TLDSettings QWidget没有出现在它的位置

有什么建议吗?

[编辑]

好,我可以确认这项工作(仍在mainwindow.cpp构造函数中):
TLDAlgoSettings = new TLDSettings();

StackedWidget = new QStackedWidget;
StackedWidget->addWidget(TLDAlgoSettings);

ui->visionSettingsVerticalLayout->addWidget(StackedWidget);

这不是一个不错的解决方法,但是我仍然想知道为什么其他选项不起作用。

最佳答案

我完整的测试代码,对我来说很好。您可以比较自己的。
使用Qt4.8.6

MainWindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "TLDSettings.h"

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_pushButton_clicked();
    void on_pushButton_2_clicked();
    void on_pushButton_3_clicked();

private:
    Ui::MainWindow *ui;
    TLDSettings *TLDAlgoSettings;
};

#endif // MAINWINDOW_H

MainWindow.cpp:
#include "MainWindow.h"
#include "ui_MainWindow.h"

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

   TLDAlgoSettings = new TLDSettings(ui->algoSettingsStackedWidget);

    ui->algoSettingsStackedWidget->addWidget(TLDAlgoSettings);
    ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
}

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

void MainWindow::on_pushButton_clicked()
{
    ui->algoSettingsStackedWidget->setCurrentIndex(0);
}

void MainWindow::on_pushButton_2_clicked()
{
    ui->algoSettingsStackedWidget->setCurrentIndex(1);
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->algoSettingsStackedWidget->setCurrentWidget(TLDAlgoSettings);
}

MainWindow.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QGridLayout" name="gridLayout">
    <item row="2" column="0">
     <layout class="QHBoxLayout" name="buttonLayout">
      <property name="spacing">
       <number>12</number>
      </property>
      <property name="leftMargin">
       <number>5</number>
      </property>
      <property name="topMargin">
       <number>5</number>
      </property>
      <property name="rightMargin">
       <number>5</number>
      </property>
      <property name="bottomMargin">
       <number>5</number>
      </property>
      <item>
       <widget class="QPushButton" name="pushButton">
        <property name="text">
         <string>Page 1</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton_2">
        <property name="text">
         <string>Page 2</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="pushButton_3">
        <property name="text">
         <string>Page 3</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item row="0" column="0">
     <widget class="QStackedWidget" name="algoSettingsStackedWidget">
      <widget class="QWidget" name="page_1">
       <layout class="QGridLayout" name="gridLayout_2">
        <item row="0" column="0">
         <layout class="QVBoxLayout" name="verticalLayout">
          <item>
           <widget class="QLabel" name="label">
            <property name="font">
             <font>
              <pointsize>12</pointsize>
             </font>
            </property>
            <property name="text">
             <string>I'm 1st page</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignCenter</set>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
      <widget class="QWidget" name="page_2">
       <layout class="QGridLayout" name="gridLayout_3">
        <item row="0" column="0">
         <layout class="QVBoxLayout" name="verticalLayout_2">
          <item>
           <widget class="QLabel" name="label_2">
            <property name="font">
             <font>
              <pointsize>12</pointsize>
             </font>
            </property>
            <property name="text">
             <string>I'm 2nd page</string>
            </property>
            <property name="alignment">
             <set>Qt::AlignCenter</set>
            </property>
           </widget>
          </item>
         </layout>
        </item>
       </layout>
      </widget>
     </widget>
    </item>
    <item row="1" column="0">
     <widget class="Line" name="line">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

TLDSettings.h:
#ifndef TLDSETTINGS_H
#define TLDSETTINGS_H

#include <QWidget>

namespace Ui {
    class TLDSettings;
}

class TLDSettings : public QWidget
{
    Q_OBJECT

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

private:
    Ui::TLDSettings *ui;
};

#endif // TLDSETTINGS_H

TLDSettings.cpp:
#include "TLDSettings.h"
#include "ui_TLDSettings.h"

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

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

TLDSettings.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>TLDSettings</class>
 <widget class="QWidget" name="TLDSettings">
  <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="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <layout class="QVBoxLayout" name="verticalLayout">
     <item>
      <widget class="QLabel" name="label">
       <property name="font">
        <font>
         <pointsize>12</pointsize>
        </font>
       </property>
       <property name="text">
        <string>I'm TLD Setting (3rd page)</string>
       </property>
       <property name="alignment">
        <set>Qt::AlignCenter</set>
       </property>
      </widget>
     </item>
    </layout>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>

关于c++ - Qt-QStackedWidget的用法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25221210/

10-09 08:26