Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > С/С++: Кроссплатформенное программирование, Qt/Gtk+/wxWidgets > Список суб-окон QMdiArea в QComboBox


Автор: loneybibi 6.6.2011, 13:21
Доброго времени суток всем !  smile 
Есть класс на основе QComboBox для отображения списка окон QMdiArea.
Подскажите пожалуйста кто знает к чему прикрутить слот обновления этого списка или как отследить добавление или удаление дочернего окна в QMdiArea что бы обновлять список каждый раз когда это происходит !? 

Код

/* ---------------------------------- */
class WindowList:public QComboBox{
    Q_OBJECT
public:
    QMdiArea *mParent;
    WindowList(QWidget *parent=0):QComboBox(parent){
        mParent=static_cast<QMdiArea*>(parent);
        if(mParent->subWindowList().count()>0){
            setEnabled(true);
            for(int i=0;i<mParent->subWindowList().count();++i)
            {
                addItem(mParent->subWindowList().at(i)->windowTitle());
            }
        }
            else
            {
                setEnabled(false);
                addItem("[Нет открытых окон]");
            }
        connect(this,SIGNAL(currentIndexChanged(int)),this,SLOT(itemActivation(int)));

        /* К ЧЕМУ ЕГО ПРИКРУТИТЬ ?????? */
        connect(mParent->subWindowList(),this,SLOT(itemListRefresh()));

    }

public slots:
    void itemActivation(int index){
        mParent->setActiveSubWindow(mParent->subWindowList().at(index));
    }

    void itemListRefresh(){
        this->clear();
        if(mParent->subWindowList().count()>0){
            setEnabled(true);
            for(int i=0;i<mParent->subWindowList().count();++i)
            {
                addItem(mParent->subWindowList().at(i)->windowTitle());
            }
        }
            else
            {
                setEnabled(false);
                addItem("[Нет открытых окон]");
            }
    }
};
/* ---------------------------------- */

Автор: loneybibi 7.6.2011, 14:28
Сам справился! (:
Получилось вроде все как хотел.
Ниже код класса и исходники готовые:
Код


/* Список окон QMdiArea в выпадающем списке QComboBox.
   В качестве родителя необходимо указать QMdiArea.

   Пример использования:
        WindowList *newList=new WindowList(ui->mdiArea);
        ui->mainToolBar->addWidget(newList);

   Примечание!
   Все дочерние окна QMdiArea должны иметь аттрибут:
        setAttribute(Qt::WA_DeleteOnClose,true);

   В противном случае окна не будут удаляться после
   закрытия, а будут просто скрываться, так же они останутся
   в списке окон и при выборе окно будет снова показано.
*/

class WindowList:public QComboBox{
    Q_OBJECT
public:
    QMdiArea *mParent;
    WindowList(QWidget *parent=0):QComboBox(parent){
        mParent=static_cast<QMdiArea*>(parent);
        this->setMinimumSize(250,NULL);
        connect(this,SIGNAL(activated(int)),this,SLOT(itemActivation(int)));
        connect(mParent,SIGNAL(subWindowActivated(QMdiSubWindow*)),this,SLOT(itemListRefresh(QMdiSubWindow*)));
        itemListRefresh(NULL);
    }

public slots:
    void itemActivation(int index){
        if(index>-1){
            mParent->setActiveSubWindow(mParent->subWindowList().at(index));
            mParent->subWindowList().at(index)->show();
            }
    }

    int searchSubWindowIndex(QMdiSubWindow* subWindow){
        int r=-1;
        if(mParent->subWindowList().size()>0){
            for(int i=0;i<mParent->subWindowList().size();++i){
                if(mParent->subWindowList().at(i)==subWindow)
                {
                    r=i;
                }
            }
        }
        return®;
    }

    void itemListRefresh(QMdiSubWindow* subWindow){
        this->clear();
        if(mParent->subWindowList().count()>0){
            setEnabled(true);
            for(int i=0;i<mParent->subWindowList().count();++i)
            {
                addItem(mParent->subWindowList().at(i)->windowTitle());
            }
            this->setCurrentIndex(searchSubWindowIndex(subWindow));
        }
            else
            {
                setEnabled(false);
                addItem("[Нет открытых окон]");
            }

    }
};
/* ------------------------------------------------ */

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)