使用QPushButton而非QComboBox更改QStackedLayout索引

Change QStackedLayout Index with QPushButton not QComboBox

本文关键字:QStackedLayout 索引 更改 QComboBox QPushButton 而非 使用      更新时间:2023-10-16

我在Qt Creator中这样做。我想更改我的QStackedLayout,只使用QPushButton,而不是QComboBox。这可能吗?有人实施过吗?我从Qt文档中得到了很多例子,但所有的例子都使用了QComboBox(现在像我需要的那样使用QPushButton)。这是我的代码:

#mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
Dialog::Dialog()
{
    QVBoxLayout *mainlayout     =   new QVBoxLayout;
    QVBoxLayout *layouta        =   new QVBoxLayout;
    QVBoxLayout *layoutb        =   new QVBoxLayout;
    QPushButton *tombola        =   new QPushButton("A");
    QPushButton *tombolb        =   new QPushButton("B");
    QPushButton *tombolc        =   new QPushButton("C");
    QFrame      *framea         =   new QFrame;
    QFrame      *frameb         =   new QFrame;
    QStackedLayout *stackia     =   new QStackedLayout;
    layouta->addWidget(tombola);
    layoutb->addWidget(tombolb);
    framea->setLayout(layouta);
    frameb->setLayout(layoutb);
    framea->setMinimumSize(88,88);
    frameb->setMinimumSize(88,88);
    //building frame
    framea->setFrameShape(QFrame::StyledPanel);
    framea->setFrameShadow(QFrame::Raised);
    frameb->setFrameShape(QFrame::StyledPanel);
    frameb->setFrameShadow(QFrame::Raised);
    //get c button smaller
    tombolc->setMaximumWidth(33);
    stackia->addWidget(framea);
    stackia->addWidget(frameb);
    stackia->addWidget(tombolc);
    mainlayout->addLayout(stackia);
    QPushButton     *tombold    =   new QPushButton("D");
    mainlayout->addWidget(tombold);
    setLayout(mainlayout);
    connect(tombold, SIGNAL(clicked()), stackia, SLOT(setCurrentIndex(1))); //CONNECTOR
}

结果

Qt创建者说:

对象::connect:没有这样的插槽QStackedLayout::setCurrentIndex(1)

我犯了什么错?

在搜索并询问了4天后的第二次机会,我将connect()和函数代码更改为:

连接器

connect(tombold, SIGNAL(clicked()), stackia, SLOT(change_stack()));

功能:void对话框::change_stack(){stackia->setCurrentIndex(1);}

结果

但Qt创建者说:

对象::connect:没有这样的插槽QStackedLayout::change_stack()

窗户立刻关上了。

在我看来,我的代码有错误。但我不知道是什么错误,所以我无法将QStackLayout内容/页面更改为另一个页面。我犯了什么错?我相信这其实很简单,但我不知道错误在哪里。

有什么建议吗?

您应该将change_stack函数添加到Dialog类中,并像这样连接到它:

class Dialog : public QWidget
{
    ...
    private slots:
    void change_stack();
private:
    QStackedLayout *stackia;
}
Dialog::Dialog
{
    ...
    connect(tombold, SIGNAL(clicked()), this, SLOT(change_stack()));
    ...
}

void Dialog::change_stack()
{
    stackia->setCurrentIndex(1);
}

关于

connect(tombold, SIGNAL(clicked()), stackia, SLOT(setCurrentIndex(1))); 

一个时隙只能具有与一个信号一样多或更少的参数。例如,你可以进行

connect( sender  , SIGNAL( somethingHappened( const QString& ) ),
         receiver, SLOT  ( doSomething      ( const QString& ) ) );

你可以做

// connect signal and ignore the parameter
connect( sender  , SIGNAL( somethingHappened( const QString& ) ),
         receiver, SLOT  ( doSomethingElse  (                ) ) );

但是你不能做

// this will not work
connect( sender  , SIGNAL( somethingElseHappened(                ) ),
         receiver, SLOT  ( doSomething          ( const QString& ) ) );

你可能想要的是这样的东西:

Dialog::Dialog
{
    ...
    // This must be a member variable
    _stackia = new QStackedLayout();
    tombola->setProperty( "tabpage", 1 );
    tombolb->setProperty( "tabpage", 2 );
    tombolc->setProperty( "tabpage", 3 );
    connect( tombola, SIGNAL( clicked       () ),
             this   , SLOT  ( tombol_clicked() ) );
    connect( tombolb, SIGNAL( clicked       () ),
             this   , SLOT  ( tombol_clicked() ) );
    connect( tombolc, SIGNAL( clicked       () ),
             this   , SLOT  ( tombol_clicked() ) );
}
// This must be defined as slot in the header
void Dialog::tombol_clicked()
{
    int index = sender()->property( "tabpage" ).toInt();
    _stackia->setCurrentIndex( index );
}