0

我在 Qt Creator 中执行此操作。我想只用 QPushButton 而不是 QComboBox 来改变我的 QStackedLayout。这可能吗?有人实施过这个吗?我从 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 Creator 说:

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

我的错误是什么?

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

连接器

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

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

结果

但 Qt Creator 说:

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

并立即关闭了窗户。

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

有什么建议吗?

4

2 回答 2

0

您应该向您的类添加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);
}
于 2013-07-29T06:35:57.263 回答
0

关于

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 );
}
于 2013-07-29T06:36:12.807 回答