Qt Widgets——子區域和子窗口


QMdiArea 一般使用於主窗口QMainWindow,用於容納多個子窗口QMdiSubWindow 

qt creator 3.0的設計師有MdiArea可直接拖入使用。

界面如下,圖中灰色框即是個MdiArea,另一圖中創建了2個QMdiSubWindow :
代碼如下:
#include "mainwindow.h"  
#include "ui_mainwindow.h"  
#include <QSize>  
MainWindow::MainWindow(QWidget *parent) :  
    QMainWindow(parent),  
    ui(new Ui::MainWindow)  
{  
    ui->setupUi(this);  
    connect(ui->actionNew,SIGNAL(triggered()),this,SLOT(creatNewWin()));//actionNew是通過設計師創建的動作  
  
}  
  
void MainWindow::creatNewWin()  
{  
    mdiWin1=new QMdiSubWindow;  
    mdiWin1->setWindowTitle("未定");  
    ui->mdiArea->addSubWindow(mdiWin1);  
    mdiWin1->resize(QSize(200,200));  
    mdiWin1->show();  
}  
MainWindow::~MainWindow()  
{  
    delete ui;  
}  

______________________________________________________________________________________________

QMdiArea
公有函數如下:
    QMdiArea(QWidget * parent = 0)  
    ~QMdiArea()  
  
QMdiSubWindow * addSubWindow(QWidget * widget, Qt::WindowFlags windowFlags = 0)  
void    removeSubWindow(QWidget * widget)  
void    setActivationOrder(WindowOrder order)//設置激活順序,默認以創建先后激活,槽函數中有調用,枚舉值見1  
void    setBackground(const QBrush & background)//設置背景,默認灰色  
void    setDocumentMode(bool enabled)  
void    setOption(AreaOption option, bool on = true)//現只有一個選項,即創建子窗口,窗口不充滿這個區域,默認是充滿的,枚舉值見2  
void    setViewMode(ViewMode mode)//設置視口模式,默認area中很多小窗口,也可以是有tabBar形式的,以下這些設置tab的函數,都需要先開啟這個。枚舉值見3  
void    setTabPosition(QTabWidget::TabPosition position)//設置tabBar的方位,有東西南北四方位,遵循地理的上北下南左西右東枚舉值見4  
void    setTabShape(QTabWidget::TabShape shape)//設置tab的形狀,默認長方形,也可以像谷歌瀏覽器那樣,梯形,枚舉值見5  
void    setTabsClosable(bool closable)//默認否,設為true時,tab上方形成一個關閉小按鈕  
void    setTabsMovable(bool movable)//設置是否可移動,默認false,可移動時,可拖動tab在tabBar上移動,現在的瀏覽器大多有這樣的功能  
  
QList<QMdiSubWindow *>    subWindowList(WindowOrder order = CreationOrder) const  
QMdiSubWindow * currentSubWindow() const  
WindowOrder activationOrder() const  
QBrush  background() const  
bool    documentMode() const  
bool    testOption(AreaOption option) const  
QMdiSubWindow * activeSubWindow() const  
QTabWidget::TabPosition tabPosition() const  
QTabWidget::TabShape    tabShape() const  
bool    tabsClosable() const  
bool    tabsMovable() const  
ViewMode    viewMode() const  
Public Slots
void    activateNextSubWindow()  
void    activatePreviousSubWindow()  
void    cascadeSubWindows()  
void    closeActiveSubWindow()  
void    closeAllSubWindows()  
void    setActiveSubWindow(QMdiSubWindow * window)  
void    tileSubWindows()//將所有子窗口在area的可視部分排列整齊  
Signals
void    subWindowActivated(QMdiSubWindow * window)//切換激活的窗口時發出  
1,enum QMdiArea::WindowOrder
Constant Value Description
QMdiArea::CreationOrder 0 按創建時的先后順序
QMdiArea::StackingOrder 1 堆疊順序
QMdiArea::ActivationHistoryOrder 2 按激活歷史前后順序.
2,enum QMdiArea::AreaOption
flags QMdiArea::AreaOptions
Constant Value Description
QMdiArea::DontMaximizeSubWindowOnActivation 0x1 激活時不使它最大化,默認是最大化的
3,QMdiArea::ViewMode
Constant Value Description
QMdiArea::SubWindowView 0 以小窗口形式顯示(default).
QMdiArea::TabbedView 1 不僅可小窗口,而且形成tabBar
4,enum QTabWidget::TabPosition
Constant Value Description
QTabWidget::North 0 上方顯示
QTabWidget::South 1
QTabWidget::West 2
QTabWidget::East 3
5,enum QTabWidget::TabShape
Constant Value Description
QTabWidget::Rounded 0 字面是圓形,但win7上更像長方形,default
QTabWidget::Triangular 1 字面三角形,說它是梯形更好些
在以上代碼中修改,圖中是執行tileSubWindows()函數后的結果:
#include "mainwindow.h"  
#include "ui_mainwindow.h"  
#include <QSize>  
#include <QTabWidget>  
#include <QBrush>  
MainWindow::MainWindow(QWidget *parent) :  
    QMainWindow(parent),  
    ui(new Ui::MainWindow)  
{  
    ui->setupUi(this);  
    QBrush b=QBrush(QColor(30,30,30),Qt::FDiagPattern);  
    ui->mdiArea->setBackground(b);  
    ui->mdiArea->setViewMode(QMdiArea::TabbedView);  
    ui->mdiArea->setTabPosition(QTabWidget::North);  
    ui->mdiArea->setTabsClosable(true);  
    ui->mdiArea->setTabsMovable(true);  
    ui->mdiArea->setTabShape(QTabWidget::Triangular);  
    ui->mdiArea->setOption(QMdiArea::DontMaximizeSubWindowOnActivation);  
  
    connect(ui->actionNew,SIGNAL(triggered()),this,SLOT(creatNewWin()));  
  
}  
  
void MainWindow::creatNewWin()  
{  
    mdiWin1=new QMdiSubWindow;  
    mdiWin1->setWindowTitle("未定");  
    ui->mdiArea->addSubWindow(mdiWin1);  
    mdiWin1->resize(QSize(200,200));  
    mdiWin1->show();  
}  
MainWindow::~MainWindow()  
{  
    delete ui;  
}  
  
void MainWindow::on_pushButton_clicked()  
{  
    ui->mdiArea->tileSubWindows();  
}  

______________________________________________________________________________________________

QMdiSubWindow
函數都比較簡單,列出如下:
   QMdiSubWindow(QWidget * parent = 0, Qt::WindowFlags flags = 0)  
    ~QMdiSubWindow()  
  
void    setKeyboardPageStep(int step)  
void    setKeyboardSingleStep(int step)  
void    setOption(SubWindowOption option, bool on = true)//未試效果,求補充,枚舉值見1  
void    setSystemMenu(QMenu * systemMenu).se.  
void    setWidget(QWidget * widget)//主要通過個函數添加它的小部件  
  
int keyboardPageStep() const  
int keyboardSingleStep() const  
QMdiArea *  mdiArea() const  
QMenu * systemMenu() const  
QWidget *   widget() const  
bool    testOption(SubWindowOption option) const  
bool    isShaded() const  

Public Slots

void showShaded()
void showSystemMenu()
Signals
void aboutToActivate()
void windowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState)


1,enum QMdiSubWindow::SubWindowOption
Constant Value Description
QMdiSubWindow::RubberBandResize 0x4 If you enable this option, a rubber band control is used to represent the subwindow's outline, and the user resizes this instead of the subwindow itself. As a result, the subwindow maintains its original position and size until the resize operation has been completed, at which time it will receive a single QResizeEvent. By default, this option is disabled.
QMdiSubWindow::RubberBandMove 0x8 If you enable this option, a rubber band control is used to represent the subwindow's outline, and the user moves this instead of the subwindow itself. As a result, the subwindow remains in its original position until the move operation has completed, at which time aQMoveEvent is sent to the window. By default, this option is disabled.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM