【问题标题】:Create a widget using QStackedLayout使用 QStackedLayout 创建一个小部件
【发布时间】:2013-08-29 16:38:53
【问题描述】:

大家好,我是使用 Qt 编程的新手,我想使用 QStackedLayout 创建一个小部件。我已经用 Qt Creator 设计了一些小部件,将它们添加到 QStackedLayout 并将其设置为主小部件。但现在我想使用 setCurrentIndex 方法使用添加的小部件内的按钮更改小部件。现在我必须使用connect 函数,但在主小部件类中,我无法从其他小部件访问组件来连接它们。那么我该怎么做呢?

#include "mainwindowwidget.h"
#include "ui_mainwindowwidget.h"


MainWindowWidget::MainWindowWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainWindowWidget)
{


    qApp->setStyleSheet("MainWindowWidget {background-color : red}");

    //initializing widgets
    this->mainWidget_ = new MainWidget;
    this->createGameWidget_ = new CreateGameWidget;
    this->widgets_ = new QStackedLayout;


    //adding widgets to QstackedLayout
    this->widgets_->addWidget(this->mainWidget_);
    this->widgets_->addWidget(this->createGameWidget_);

    this->setLayout(this->widgets_);
    this->showFullScreen();
    // I would like to connect the qstackedlayout
    // = widgets_ with a button placed in mainwidget_
    ui->setupUi(this);

}

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

【问题讨论】:

    标签: qt layout widget


    【解决方案1】:

    这里有几个选项。如果您的按钮是MainWidget 的公共成员,那么您只需将按钮的clicked() 信号连接到MainWindow 中的插槽即可。

    //mainwindow.h
    ...
    public slots:
       void buttonClicked();
    

     

    //mainwindow.cpp
    ...
       connect(mainWidget_->button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    ...
    void buttonClicked()
    {
       //do what you want to do here...
    }
    

    另一种选择是在 MainWidget 类中创建自定义信号。然后将按钮的clicked() 信号连接到这个自定义信号:

    //mainwidget.h
    ...
    signals:
       void buttonClickedSignal();
    

     

    //mainwidget.cpp
       connect(button, SIGNAL(clicked()), this, SIGNAL(buttonClickedSignal()));
    

    然后将buttonClickedSignal() 信号连接到MainWindow 中的插槽:

    //mainwindow.cpp
       connect(mainWidget_, SIGNAL(buttonClickedSignal()), this, SLOT(buttonClicked()));
    

    第三种选择是向您的MainWidget 类添加一个函数,该函数返回一个指向您的按钮的指针。然后在 MainWindow 类中调用此函数,并使用该指针将按钮连接到插槽。

    //mainwidget.h
    ...
    public:
       QPushButton* getButton();
    ...
    

      

    //mainwdiget.cpp
    ...
    QPushButton* getButton()
    {
       return button;
    }
    ...
    

     

    //mainwindow.cpp
    ...
       QPushButton *button = mainWidget_->getButton();
       connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
    

    【讨论】:

      【解决方案2】:

      来自 Qt 帮助

      The QStackedLayout class provides a stack of widgets where only one widget is visible at a time.
      

      所以传递索引是识别小部件需要在特定时间点显示在 StackedLayout 上的关键。假设您的信号名称是在 mainWidget_ 和 createGameWidget_ 中声明的“activated(int)”

      所以你需要这样连接

      //MainWindowWidget class.
      connect(MainWidget, SIGNAL(activated(int)), widgets_ , SLOT(setCurrentIndex(int)));
      connect(createGameWidget_, SIGNAL(activated(int)), widgets_ , SLOT(setCurrentIndex(int)));
      
         //In MainWidget class you need to emit signal
          MainWidget::ChangeLayout()
          {
              emit activated(1); //createGameWidget_will be displayed
          }
      
         //In createGameWidget_class you need to emit signal
          createGameWidget_::ChangeLayout()
          {
              emit activated(0); //MainWidget will be displayed
          }
      

      【讨论】:

        猜你喜欢
        • 2015-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-08
        • 1970-01-01
        • 2012-02-04
        • 2011-05-12
        • 2010-12-22
        相关资源
        最近更新 更多