QSignalMapper 使用方法 参见QSignalMapper中使用文档

TestServiceDlg1::TestServiceDlg1(const QStringList &texts,QWidget* parent)
    : QWidget(parent)
{
    this->setObjectName(QObject::tr("TestServiceDlg1"));
    m_pageRes.setupUi(this);//!  Ui::pageCustomizePopular 是UI界面对象

    signalMapper = new QSignalMapper(this);

        QGridLayout *gridLayout = new QGridLayout;
        for (int i = 0; i < texts.size(); ++i) {
            QPushButton *button = new QPushButton(texts[i]);

            connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
            signalMapper->setMapping(button, texts[i]);

            m_pageRes.gridLayout_AllParams->addWidget(button, i / 3, i % 3);
        }

        connect(signalMapper, SIGNAL(mapped(QString)),
                this, SIGNAL(testclicked(QString)));

        connect(this, SIGNAL(testclicked(QString)),
                this, SLOT(testclickedSlot(QString)));

        //setLayout(gridLayout);
}
void TestServiceDlg1::testclickedSlot(QString s)
{
    QMessageBox msgBox;
    msgBox.setText(s);
    msgBox.exec();

}

class TestServiceDlg1 : public QWidget
{
    Q_OBJECT
public:
    explicit TestServiceDlg1(const QStringList &texts, QWidget* parent = NULL);
    virtual ~TestServiceDlg1();
public:
    Ui::TestServiceDlgUI1 m_pageRes; //!  Ui::pageCustomizePopular 是UI界面对象
private:
    Q_DISABLE_COPY(TestServiceDlg1)
    QSignalMapper* signalMapper;

 protected Q_SLOTS:
signals:
    void testclicked(const QString &text);
private slots:
    void testclickedSlot(QString s);
};

下面是用QACTION完成 QStackedWidget 的界面切换
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "ribbonwindow.h"
#include 
#include 
#include 
#include 

#include "TestServiceDlg1.h"
#include "TestServiceDlg2.h"
#include "TestServiceDlg3.h"
#include "TestServiceDlg4.h"


QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QSignalMapper;
class QRadioButton; 
QT_END_NAMESPACE


/* MainWindow */
class MainWindow : public RibbonWindow
{
    Q_OBJECT
public:
    MainWindow();
    virtual ~MainWindow();

protected:
    virtual void closeEvent(QCloseEvent* event);

private Q_SLOTS:
    void switchFrameTheme(bool);
    void setActiveSubWindow(QWidget *window);
private:
    void initRibbonBar();//!初始化RibbonBar以及增加page
    void createRibbonPage_1();
    void createRibbonPage_2();
    void createRibbonPage_3();
    void createPage_1Group_1(Qtitan::RibbonPage *page);
    void createPage_2Group_1(RibbonPage *page);
    void createPage_3Group_1(Qtitan::RibbonPage *page);
private:
    RibbonPage* m_page1;
    RibbonPage* m_page2;
    RibbonPage* m_page3;

    QAction* m_ActionPage_1Group_1;
    QAction* m_ActionPage_2Group_1;
    QAction* m_ActionPage_3Group_1;

    QSignalMapper* m_windowMapper;

    TestServiceDlg1 *m_dlg1;
    TestServiceDlg2 *m_dlg2;
    TestServiceDlg3 *m_dlg3;

    QStackedWidget *m_StackedWidget;
    Qt::WindowStates m_stateWindow;
};

#endif

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

#include "mainwindow.h"
#include "aboutdialog.h"


/* ManagerRibbon */
MainWindow::MainWindow()
    : RibbonWindow(Q_NULL)
{
    setWindowTitle(tr("Ribbon MDI"));
    setUnifiedTitleAndToolBarOnMac(true);

    //ribbonBar()->setTitleBarVisible(false);

   // m_stateWindow = windowState();

    //QRect geom = QApplication::desktop()->availableGeometry();
    //resize( 2 * geom.width() / 3, 2 * geom.height() / 3 );

    m_StackedWidget = new QStackedWidget(this);

    m_windowMapper = new QSignalMapper(this);
    connect(m_windowMapper, SIGNAL(mapped(QWidget*)), this, SLOT(setActiveSubWindow(QWidget*)));

    m_dlg1=new TestServiceDlg1(this);
    m_dlg2=new TestServiceDlg2(this);
    m_dlg3=new TestServiceDlg3(this);

    m_StackedWidget->addWidget(m_dlg1);
    m_StackedWidget->addWidget(m_dlg2);
    m_StackedWidget->addWidget(m_dlg3);

    initRibbonBar();
    ribbonBar()->setFrameThemeEnabled();
	switchFrameTheme(true);
    this->setCentralWidget(m_StackedWidget);
    m_stateWindow = windowState();

    QDesktopWidget* desktopWidget = QApplication::desktop();
    QRect clientRect = desktopWidget->availableGeometry();
    QRect applicationRect = desktopWidget->screenGeometry();

    //clientRect.moveBottom();

    this->setGeometry(clientRect);
    this->move(0,3);
    //setWindowState(Qt::WindowActive);
}

MainWindow::~MainWindow()
{
}

void MainWindow::setActiveSubWindow(QWidget* window)
{
    if (!window)
        return;
    m_StackedWidget->setCurrentWidget(window);
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    qApp->closeAllWindows();;
}

void MainWindow::switchFrameTheme(bool checked)
{
//    showNormal();
    ribbonBar()->setFrameThemeEnabled(checked);
}


void MainWindow::initRibbonBar()
{
    createRibbonPage_1();
    createRibbonPage_2();
    createRibbonPage_3();
}

//! 创建服务管理PAGE
void MainWindow::createRibbonPage_1()
{
    const QString ltext=tr("Page_1");
    m_page1 = ribbonBar()->addPage(ltext);
    if (m_page1)
    {
        createPage_1Group_1(m_page1);//! pageServiceManage下面组的添加
    }
}

void MainWindow::createRibbonPage_2()
{
    m_page2 = ribbonBar()->addPage(tr("Page_2"));
    if (m_page2)
    {
        createPage_2Group_1(m_page2);//! pageServiceManage下面组的添加
    }
}

void MainWindow::createRibbonPage_3()
{
    const QString ltext=tr("Page_3");//psl 服务器监控
    m_page3 = ribbonBar()->addPage(ltext);
    if (m_page3)
    {
        createPage_3Group_1(m_page3);//! pageServiceManage下面组的添加
    }
}

void MainWindow::createPage_1Group_1(Qtitan::RibbonPage* page)
{
    if (Qtitan::RibbonGroup* gpPage_1Group_1 = page->addGroup(::qtnIcon(Image_Clipboard), tr("Page_1Group_1")))
    {
        gpPage_1Group_1->setOptionButtonVisible();
        m_ActionPage_1Group_1 = gpPage_1Group_1->addAction(qtnIcon(":/res/qtitan.png", ":/res/qtitan.png"), tr("Page_1Group_1"), Qt::ToolButtonTextUnderIcon);
        RibbonControl* control  = gpPage_1Group_1->controlByAction(m_ActionPage_1Group_1);
        control->sizeDefinition(RibbonControlSizeDefinition::GroupPopup)->setImageSize(RibbonControlSizeDefinition::ImageLarge);

        connect(m_ActionPage_1Group_1, SIGNAL(triggered()), m_windowMapper, SLOT(map()));
        m_windowMapper->setMapping(m_ActionPage_1Group_1, m_dlg1);
    }
}

void MainWindow::createPage_2Group_1(RibbonPage *page)
{
    if (Qtitan::RibbonGroup* gpPage_2Group_1 = page->addGroup(::qtnIcon(Image_Clipboard), tr("Page_2Group_1")))
    {
        gpPage_2Group_1->setOptionButtonVisible();

        m_ActionPage_2Group_1 = gpPage_2Group_1->addAction(QIcon(qtnIcon(":/res/qtitan.png", ":/res/qtitan.png")),  tr("Page_2Group_1"), Qt::ToolButtonTextUnderIcon);

        RibbonControl* control  = gpPage_2Group_1->controlByAction(m_ActionPage_2Group_1);
        control->sizeDefinition(RibbonControlSizeDefinition::GroupPopup)->setImageSize(RibbonControlSizeDefinition::ImageLarge);
        connect(m_ActionPage_2Group_1, SIGNAL(triggered()), m_windowMapper, SLOT(map()));
        m_windowMapper->setMapping(m_ActionPage_2Group_1, m_dlg2);
    }
}

void MainWindow::createPage_3Group_1(Qtitan::RibbonPage* page)
{
    if (Qtitan::RibbonGroup* gpPage_3Group_1 = page->addGroup(::qtnIcon(Image_Clipboard), tr("Page_3Group_1")))
    {
        gpPage_3Group_1->setOptionButtonVisible();
        m_ActionPage_3Group_1 = gpPage_3Group_1->addAction(qtnIcon(":/res/qtitan.png", ":/res/qtitan.png"), tr("Page_3Group_1"), Qt::ToolButtonTextUnderIcon);
        RibbonControl* control  = gpPage_3Group_1->controlByAction(m_ActionPage_3Group_1);
        control->sizeDefinition(RibbonControlSizeDefinition::GroupPopup)->setImageSize(RibbonControlSizeDefinition::ImageLarge);
        connect(m_ActionPage_3Group_1, SIGNAL(triggered()), m_windowMapper, SLOT(map()));
        m_windowMapper->setMapping(m_ActionPage_3Group_1, m_dlg3);
    }
}






 
  
 
 

你可能感兴趣的:(QSignalMapper 使用方法 参见QSignalMapper中使用文档)