【问题标题】:Change QStackedLayout Index with QPushButton not QComboBox使用 QPushButton 而不是 QComboBox 更改 QStackedLayout 索引
【发布时间】:2016-04-22 20:06:47
【问题描述】:

我在 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: No such slot QStackedLayout::setCurrentIndex(1)

我的错误是什么?

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

连接器

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

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

结果

但是 Qt Creator 说:

Object::connect: No such slot QStackedLayout::change_stack()

然后立即关闭窗口。

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

有什么建议吗?

【问题讨论】:

    标签: c++ qt user-interface


    【解决方案1】:

    您应该将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);
    }
    

    【讨论】:

    • 谢谢。但是我的 mainwindow.cpp 还是一样的tempel.blankon.in/146191。我已经在我的 mainwindow.h 上实现了你的建议,所以我把它改成了:tempel.blankon.in/146192。但确实Qt Creator说:错误:Dialog::change_stack()' <br/> first defined here <br/> In function Dialog::change_stack()'的多重定义:
      错误:`Dialog::change_stack()'的多重定义
      我的代码错误是什么?
    【解决方案2】:

    关于

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-24
      • 2014-02-26
      • 2016-11-13
      • 2012-10-19
      • 2017-04-01
      • 1970-01-01
      • 2013-01-12
      相关资源
      最近更新 更多