C++ 使用QPushButton而不是QComboBox更改QStackedLayout索引
我在Qt Creator中这样做。我只想用QPushButton而不是QComboBox来更改我的QStackedLayout。这可能吗?有人实施过这个吗?我从Qt文档中得到了很多示例,但所有示例都使用QComboBox(现在是我需要的QPushButton)。这是我的代码:C++ 使用QPushButton而不是QComboBox更改QStackedLayout索引,c++,qt,user-interface,C++,Qt,User Interface,我在Qt Creator中这样做。我只想用QPushButton而不是QComboBox来更改我的QStackedLayout。这可能吗?有人实施过这个吗?我从Qt文档中得到了很多示例,但所有示例都使用QComboBox(现在是我需要的QPushButton)。这是我的代码: #mainwindow.cpp #include "mainwindow.h" #include "ui_mainwindow.h" Dialog::Dialog() { QVBoxLayout *mainlay
#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创建者说:
Object::connect:没有这样的插槽QStackedLayout::setCurrentIndex(1)
我犯了什么错
在搜索和询问4天后,我再次将connect()和函数代码更改为:
连接器:
connect(tombold, SIGNAL(clicked()), stackia, SLOT(change_stack()));
功能:
void Dialog::change_stack()
{
stackia->setCurrentIndex(1);
}
结果
但Qt创建者说:
对象::连接:没有这样的插槽QStackedLayout::更改\u堆栈()
窗户立刻关上了
在我看来,我的代码有错误。但我不知道发生了什么错误,因此无法将布局内容/页面更改为其他页面。我犯了什么错?我相信这其实很简单,但我不知道错误在哪里
有什么建议吗?您应该将
change\u stack
函数添加到您的对话框
类中,并像这样连接到它:
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);
}
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 );
}
关于
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 ( ) ) );
你能做到吗
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& ) ) );
您可能想要的是这样的:
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);
}
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 );
}
非常感谢。但是我的mainwindow.cpp仍然是一样的。我已经在我的主窗口上实现了您的建议。h因此我将其更改为:。但实际上Qt创建者说:错误:Dialog::change_stack()的多个定义
首先在函数中定义
Dialog::change_stack():
错误:Dialog::change_stack()的多个定义
我在代码上的错误是什么?