Qt Widgets——子区域和子窗口

本文详细介绍了如何在QT Creator中使用MdiArea组件,包括其基本使用方法、属性设置、视口模式调整、TabBar配置以及相关子窗口QMdiSubWindow的创建与管理。通过实例代码演示了如何添加、布局、激活、关闭子窗口,并展示了不同视口模式和TabBar样式的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
ConstantValueDescription
QMdiArea::CreationOrder0按创建时的先后顺序
QMdiArea::StackingOrder1堆叠顺序
QMdiArea::ActivationHistoryOrder2按激活历史前后顺序.
2,enum QMdiArea::AreaOption
flags QMdiArea::AreaOptions
ConstantValueDescription
QMdiArea::DontMaximizeSubWindowOnActivation0x1激活时不使它最大化,默认是最大化的
3,QMdiArea::ViewMode
ConstantValueDescription
QMdiArea::SubWindowView0以小窗口形式显示(default).
QMdiArea::TabbedView1不仅可小窗口,而且形成tabBar
4,enum QTabWidget::TabPosition
ConstantValueDescription
QTabWidget::North0上方显示
QTabWidget::South1
QTabWidget::West2
QTabWidget::East3
5,enum QTabWidget::TabShape
ConstantValueDescription
QTabWidget::Rounded0字面是圆形,但win7上更像长方形,default
QTabWidget::Triangular1字面三角形,说它是梯形更好些
在以上代码中修改,图中是执行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

Signals
voidaboutToActivate()
voidwindowStateChanged(Qt::WindowStates oldState, Qt::WindowStates newState)


1,enum QMdiSubWindow::SubWindowOption
ConstantValueDescription
QMdiSubWindow::RubberBandResize0x4If 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::RubberBandMove0x8If 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.

### 创建按钮并实现点击事件打开新窗口Qt Widgets Designer 中创建一个按钮,并通过 Python 脚本处理其点击事件来打开新的窗口,涉及多个步骤。首先,在 Qt Designer 中设计主窗口窗口UI 文件。 #### 设计UI文件 1. 启动 Qt Designer 并新建两个窗体:`main_window.ui` `new_window.ui`。 2. 在 `main_window.ui` 添加 QPushButton 组件作为触发新开窗口动作的按钮[^1]。 3. 设置按钮属性,如对象名称(objectName),以便后续代码中引用该组件。 #### 编写Python脚本来加载UI并与逻辑关联 为了使上述设计生效,需编写相应的 Python 代码连接这些界面元素到实际功能上: ```python import sys from PyQt5 import uic from PyQt5.QtWidgets import QApplication, QMainWindow class MainWindow(QMainWindow): def __init__(self): super().__init__() # 加载 main_window.ui 文件定义的内容 self.main_ui = uic.loadUi('path_to_main_window.ui', self) # 将按钮点击信号与槽函数相连 self.pushButton.clicked.connect(self.open_new_window) def open_new_window(self): """当按下按钮时调用此方法""" new_win = NewWindow() new_win.show() class NewWindow(QMainWindow): def __init__(self): super().__init__() # 加载 new_window.ui 文件定义的内容 self.new_ui = uic.loadUi('path_to_new_window.ui', self) if __name__ == '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) ``` 这段程序展示了如何利用 PyQt5 库中的 `uic.loadUi()` 函数读取由 Qt Designer 创造出来的 `.ui` 文件,并将其转换成可操作的对象实例;同时实现了按钮点击后的响应机制——即显示另一个独立运行的新窗口[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值