QT5:窗口界面框架

 

复制代码
在项目Application中:

QT Widgets Application(桌面QT应用)

QT Console Application(控制台QT应用)

QT for Python-Empty / Window(用Python开发QT应用)

QT Quick Application-Empty / Scroll / Stack /Swipe(移动平台开发QT应用)
复制代码

 

一.QWidget

 

Widget 窗口主要是在上面放置布局和控件,可以嵌入到主窗体中

 

复制代码
QWidget widget;
widget.setWindowTitle(QObject::tr("k5"));
widget.resize(400, 300);
widget.move(200, 100);
widget.show();

int x = widget.x();
qDebug("x:%d", x);

int y = widget.y();
qDebug("y:%d", y);
复制代码

 

1.窗体框架

 

复制代码
// widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H
复制代码

 

复制代码
// widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}
复制代码

 

复制代码
// main.cpp

#include "widget.h"
#include <QApplication>

int main(int argc, char *argv[])
{  //主应用程序对象,只能有一个
    QApplication a(argc, argv);  //继承父类QWidget
    Widget w;  //窗口对象默认不显示,必须调用show()方法显示窗口
    w.show();
  //进行消息循环,后面代码没用
    return a.exec();
}
复制代码

 

2.配置

 

复制代码
//QTTest.pro

QT    +=    core gui

greaterThan(QT_MAJOR_VERSION, 4) : QT += widgets

CONFIG    +=    c++11

DEFINES   +=    QT_DEPRECATED_WARNINGS

SOURCE    +=    \
    main.cpp    \
    mainwindow.cpp

HEADERS    +=    \
    mainwindow.h

FORMS    +=    \
    mainwindow.ui

TRANSLATIONS    +=    \
    QTTest_zh_CN.ts

qnx    :    target.path    =    /tmp/&&{TARGET}/bin
else    :    unix     :!    android    :    target.path    =    /opt/$${TARGET}/bin
!isEmpth(target.path)    :    INSTALLS    +=    target
复制代码

 

复制代码
//mainwindow.ui
<?xml version = "1.0" encoding = "UTF-8"?>
<ui version = "4.0">
    <class> MainWindow </class>
    <widget class = "QMainWindow" name = "MainWindow">
        <property name = "geometry">
            <rect>
                <x>0</x>
                <y>0</y>
                <width>800</width>
                <height>600</height>
            </rect>
         </property>
         <property name = "windowTitle">
             <string>Maindow</string>
         </property>
     <widget class = "QWidget" name = "centralwidget"/>
     <widget class = "QMenuBar" name = "menubar">
         <property name = "geometry">
             <rect>
                <x>0</x>
                <y>0</y>
                <width>800</width>
                <height>26</height>
             </rect>
          </property>
      </widget>
      <widget class = "QStatusBar" name = "statusbar" />
      </widget>
      <resources/>
      <connections/>
</ui>
复制代码

 

3.成员函数

 

复制代码
QWidget 类继承于 QObject 类和 QPaintDevice 类

void QWidget::resize(int w, int h)

void QWidget::resize(const QSize& )

 

QSize QWidget::size() const

void QWidget::show()


QWidget::x()

QWidget::y()

QWidget::pos()

QWidget::find()

QWidget::font()

QWidget::grab()

QWidget::hide()

QWidget::mask()

QWidget::move()

QWidget::rect()

QWidget::close()

QWidget::lower()

QWidget::mapTo()

QWidget::raise()

QWidget::style()

QWidget::winId()

QWidget::cursor()

QWidget::layout()

QWidget::locale()

QWidget::render()

 

QWidget::scroll()

QWidget::update()

QWidget::window()

QWidget::actions()

QWidget::childAt()

QWidget::isModal()

QWidget::mapFrom()

QWidget::palette()

QWidget::repaint()

QWidget::setFont()

QWidget::setMask()

QWidget::toolTip()

QWidget::baseSize()

QWidget::fontInfo()

QWidget::geometry()

QWidget::hasFocus()

QWidget::isHidden()

QWidget::isWindow()

QWidget::setFocus()

QWidget::setStyle()

QWidget::sizeHint()

QWidget::addAction()

QWidget::clearMask()

QWidget::framwSize()

QWidget::grabMouse()

QWidget::isEnabled()

QWidget::isVisible()

QWidget::setCursor()

QWidget::setHidden()

QWidget::setLayout()

QWidget::setLocale()

QWidget::statusTip()

QWidget::whatsThis()

QWidget::addActions()

QWidget::adjustSize()

QWidget::clearFocus()

QWidget::focusProxy()

QWidget::isTopLevel()

QWidget::setEnabled()

QWidget::setPalette()

QWidget::setToolTip()

QWidget::setVisible()

QWidget::showNormal()

QWidget::sizePolicy()

QWidget::stackUnder()

QWidget::styleSheet()

QWidget::underMouse()
复制代码

 

二.QDialog

 

1.窗体框架

 

QDialog类继承与QWidget类

注意:添加hellodialog.ui文件的时候,要把里面的 <class>HelloDialog</class>

                       <widget class = "QDialog" name = "HelloDialog"> 改过来,不然会无法识别 ui 这个指针

 

复制代码
// hellodialog.h

#ifndef HELLODIALOG_H
#define HELLODIALOG_H

#include <QDialog>

namespace Ui{
    class HelloDialog;
}

class HelloDialog : public QDialog{

    Q_OBJECT
public:
    explicit HelloDialog(QWidget* parent = nullptr);
    ~HelloDialog();

private:
    Ui::HelloDialog* ui;
};


#endif // HELLODIALOG_H
复制代码

 

复制代码
// hellodialog.cpp#include "hellodialog.h"
#include "ui_hellodialog.h"


HelloDialog::HelloDialog(QWidget* parent) :
    QDialog(parent),
    ui(new Ui::HelloDialog)
{
    ui->setupUi(this);

}

HelloDialog::~HelloDialog()
{
    delete ui;
}
复制代码

 

复制代码
// main.cpp

#include "hellodialog.h"
#include <QApplication>

int main(int argc, char* argv[])
{
    QApplication a(argc, argv);
    HelloDialog w;
    w.show();

    return a.exec();
}
复制代码

 

2.附加窗口

 

//模态对话框

QDialog* dialog = new QDialog(this);
dialog->setModal(true);
dialog->show();QDialog dialog;dialog.setModal(true);dialog.show()

 

//非模态对话框

QDialog* dialog = new QDialog(this);
dialog->show();

QDialog dialog;
dialog.show()

 

3.对话框状态

 

QDialog dialog;
if (dialog.exec() == QDialog::Accepted) 

 

QDialog类是所有对话框窗口类,对话框窗口是一个经常用来完成短小任务或者和用户进行简单交互的顶层窗口

对话框分为模态对话框和非模态对话框

模态对话框在关闭它之前,不能与同一个应用程序的其他窗口进行交互

非模态对话框既可以和它交互,也可以和同一个应用程序的其他窗口交互

 

模态对话框用 exec() 函数显示,或者在 show() 函数之前加上 setModal(true)

非模态对话框用 show() 函数显示

 

复制代码
// dialog.h

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    explicit Dialog(QWidget *parent = nullptr);
    ~Dialog();

private:
    Ui::Dialog *ui;
};

#endif // DIALOG_H
复制代码

 

复制代码
// dialog.cpp

#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}

Dialog::~Dialog()
{
    delete ui;
}
复制代码

 

复制代码
// main.cpp

#include "dialog.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    Dialog w;
    // w.setModal(true);
    // w.exec();
    w.show();

  
    return a.exec();
}
复制代码

 

三.QMainWindow

 

MainWindow类提供一个有菜单条 工具栏 状态条的主应用程序窗口

它是最常见的GUI主窗口形式,它由外到内依次是菜单栏 状态栏 工具栏 停靠窗口 中心窗口

 

 

复制代码
//One.pro

#-------------------------------------------------
#
# Project created by QtCreator 2019-06-18T13:29:09
#
#-------------------------------------------------
 
# 表示项目加入core gui模块,用于GUI设计的类库模块
QT       += core gui
 
# 条件执行语句,当QT主版本大于4才加入widgets模块
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
 
# 生成的目标可执行文件名称
TARGET = One
 
# 项目使用的模板是app,一般应用程序
TEMPLATE = app
 
DEFINES += QT_DEPRECATED_WARNINGS
 
CONFIG += c++11
 
#QT会自动修改以下新增/删除文件
SOURCES += \
        main.cpp \
        mainwindow.cpp
 
HEADERS += \
        mainwindow.h
 
FORMS += \
        mainwindow.ui
 
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
复制代码

 

复制代码
//mainwindow.h

QWidget是所有用户界面对象的基类,QMainWindow和QDialog都是QWidget的子类

QMainWindow类提供一个菜单条/工具条/状态条的主应用程序窗口

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
 
#include <QMainWindow>
 
namespace Ui {
class MainWindow;
}
 
class MainWindow : public QMainWindow
{
    Q_OBJECT
 
public:
    explicit MainWindow(QWidget *parent = nullptr);
    ~MainWindow();
 
private:
    Ui::MainWindow *ui;
};
 
#endif // MAINWINDOW_H
复制代码

 

复制代码
//mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
 
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}
 
MainWindow::~MainWindow()
{
    delete ui;
}
复制代码

 

复制代码
//main.cpp

#include "mainwindow.h"
#include <QApplication>
 
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
 
    return a.exec();
}
复制代码

 

//hellodialog.ui

可视化设计的窗体的定义文件,是一个XML文件

 

复制代码
MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent)
{
  //重置窗口大小
  resize(600, 400);

  //菜单栏创建
  QMenuBar* bar = menuBar();

  //将菜单栏放入窗口中
  setMenuBar(bar);

  //创建菜单
  QMenu* fileMenu = bar->addMenu("文件");
  QMenu* editMenu = bar->addMenu("编辑");

  //创建菜单项
  QAction* newAction = fileMenu->addAction("新建");

  //添加分隔符
  fileMenu->addSeparator();

  //
  QAction* openAction = fileMenu->addAction("打开");

  
  //工具栏可以有多个
  QToolBar* toolBar = new QToolBar(this);
  addToolBar(Qt::LeftToolBarArea, toolBar);

  //后期设置 只允许左右停靠
  toolBar->setAllowedAreas(Qt::LeftToolBarArea | Qt::RightToolBarArea);

  //设置浮动
  toolBar->setFloatable(false);

  //设置移动
  toolBar->setMovable(false);

  //工具栏可以设置内容
  toolBar->addAction(newAction);
 
  //添加分割线
  toolBar->addSeparator();
  
  toolBar->addAction(openAction);

  //工具栏中添加控件
  QPushButton* btn = new QPushButton("a", this);
  toolBar->addWidget(btn);
      

}
复制代码

 

posted @   言午丶  阅读(499)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示