• 6183阅读
  • 1回复

[提问]如何获取 mdi 程序子窗口信息 [复制链接]

上一主题 下一主题
离线hx0hx
 

只看楼主 倒序阅读 楼主  发表于: 2011-05-29
请帮忙看看,为什么 updateActions() 无法按期望工作呢

#include <QtGui>

#include "mdi.h"



void MdiWindow::updateActions()
{
    QMdiSubWindow *subWindow = mdiArea->activeSubWindow();
    if (subWindow) {
        QLabel *tempLabel = qobject_cast<QLabel *>(subWindow->widget());
        if (tempLabel->text() == aLabel->text()) {
            aAction->setChecked(true);
        } else if (tempLabel->text() == bLabel->text()) {
            bAction->setChecked(true);
        }
    }
    //如何判断某个子窗口是激活状态呢
    //aAction->setChecked(aSubWindow->hasFocus());
    //bAction->setChecked(bSubWindow->hasFocus());
}

MdiWindow::MdiWindow()
{
    mdiArea = new QMdiArea;
    setCentralWidget(mdiArea);
    connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindw *)),
            this, SLOT(updateActions()));

    aLabel = new QLabel(tr("标签 a"));
    bLabel = new QLabel(tr("标签 b"));

    //create actions
    aAction = new QAction(tr("标签 a"), this);
    aAction->setCheckable(true);
    connect(aAction, SIGNAL(triggered()), aLabel, SLOT(show()));
    connect(aAction, SIGNAL(triggered()), aLabel, SLOT(setFocus()));

    bAction = new QAction(tr("标签 b"), this);
    bAction->setCheckable(true);
    connect(bAction, SIGNAL(triggered()), bLabel, SLOT(show()));
    connect(bAction, SIGNAL(triggered()), bLabel, SLOT(setFocus()));

    //create menus
    windowMenu = menuBar()->addMenu(tr("&Window"));
    windowMenu->addAction(aAction);
    windowMenu->addAction(bAction);

    lActionGroup = new QActionGroup(this);
    lActionGroup->addAction(aAction);
    lActionGroup->addAction(bAction);

    //create toolbars

    //create context menu

    //create statusbar

    //add sub window
    aSubWindow = mdiArea->addSubWindow(aLabel);
    bSubWindow = mdiArea->addSubWindow(bLabel);
    mdiArea->cascadeSubWindows();    //Arranges all the child windows in a cascade pattern.
    //mdiArea->activateNextSubWindow();

    //setWindowIcon(QPixmap(":/images/hx.png"));
    setWindowTitle(tr("mdi 程序"));
}




离线hx0hx

只看该作者 1楼 发表于: 2011-05-31
我改好了
我改成这样子了



#include <QtGui>


#include "mdi.h"


MdiWindow::MdiWindow()
{
    mdiArea = new QMdiArea;
    setCentralWidget(mdiArea);
    //this not work
    //connect(mdiArea, SIGNAL(subWindowActivated(QMdiSubWindw *)), this, SLOT(updateActions()));


    aLabel = new QLabel(tr("标签 a"));
    bLabel = new QLabel(tr("标签 b"));
    statusLabel = new QLabel(tr(""));
    count = 0;


    //create actions
    aAction = new QAction(tr("标签 a"), this);
    aAction->setCheckable(true);
    connect(aAction, SIGNAL(triggered()), aLabel, SLOT(show()));
    connect(aAction, SIGNAL(triggered()), aLabel, SLOT(setFocus()));


    bAction = new QAction(tr("标签 b"), this);
    bAction->setCheckable(true);
    connect(bAction, SIGNAL(triggered()), bLabel, SLOT(show()));
    connect(bAction, SIGNAL(triggered()), bLabel, SLOT(setFocus()));


    //create menus
    windowMenu = menuBar()->addMenu(tr("&Window"));
    windowMenu->addAction(aAction);
    windowMenu->addAction(bAction);


    lActionGroup = new QActionGroup(this);
    lActionGroup->addAction(aAction);
    lActionGroup->addAction(bAction);


    //create toolbars


    //create context menu


    //create statusbar
    statusBar()->addWidget(statusLabel);


    //add sub window
    aSubWindow = mdiArea->addSubWindow(aLabel);
    connect(aSubWindow, SIGNAL(aboutToActivate()), this, SLOT(updateActions()));
    bSubWindow = mdiArea->addSubWindow(bLabel);
    connect(bSubWindow, SIGNAL(aboutToActivate()), this, SLOT(updateActions()));
    mdiArea->cascadeSubWindows();    //Arranges all the child windows in a cascade pattern.
    //mdiArea->activateNextSubWindow();


    //setWindowIcon(QPixmap(":/images/hx.png"));
    setWindowTitle(tr("mdi 程序"));
}


void MdiWindow::updateActions()
{
    statusLabel->setText(tr("window activated %1 times").arg(++count));
    
    aAction->setChecked(aSubWindow->windowState() & Qt::WindowActive);
    bAction->setChecked(bSubWindow->windowState() & Qt::WindowActive);
}


        
快速回复
限100 字节
 
上一个 下一个