我正在尝试切换堆叠的按钮,如下面的应用程序所示。
宣言:
#include <QPushButton>
#include <QMainWindow>
#include <QStackedWidget>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
private:
QPushButton* m_button[2];
QStackedWidget *m_buttonStack;
};
实施(注意连接):
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
resize(300, 300);
m_buttonStack = new QStackedWidget( this );
m_buttonStack->setGeometry( 100, 100 , 100 , 100 );
m_button[0] = new QPushButton( this );
m_button[0]->setText( "Button 1" );
m_buttonStack->addWidget( m_button[0] );
m_button[1] = new QPushButton( this );
m_button[1]->setText( "Button 2" );
m_buttonStack->addWidget( m_button[1] );
m_buttonStack->setCurrentWidget( m_button[1] );
QObject::connect( m_button[0] , SIGNAL( clicked() ) , m_buttonStack , SLOT( setCurrentWidget( m_button[1] ) ) );
QObject::connect( m_button[1] , SIGNAL( clicked() ) , m_buttonStack , SLOT( setCurrentWidget( m_button[0] ) ) );
}
由于某些莫名其妙的原因,虽然QStackedWidget::setCurrentWidget
定义QObject::connect
无法连接到它,但从应用程序输出窗口复制的以下错误消息显示:
Object::connect: No such slot QStackedWidget::setCurrentWidget( m_button[1] ) in ..\mainwindow.cpp:21
Object::connect: No such slot QStackedWidget::setCurrentWidget( m_button[0] ) in ..\mainwindow.cpp:22