QT系列第1节 QT中窗口使用简介

     QT中窗口按照使用场景分类,可以分成两大类,一类是独立显示的窗口,主要是 QWidget ,QDialog ,QMainWindow,一类是嵌套在别的窗口的子窗口,包含各种控件以及子窗口,窗口类图如下:

QT系列第1节 QT中窗口使用简介_第1张图片

本篇文章只介绍 QWidget  QDialog  QMainWindow 这三个类。

目录

1. QWidget类

1.1 构造方法

1.2 public方法

1.3 信号

1.4 槽

2. QMainWindow类

2.1 构造方法

2.2 public方法

2.3 信号

2.4 槽

3. QDialog类

3.1 构造方法

3.2 public方法

3.3 信号

3.4 槽

4.使用举例

4.1 Qt Creator使用注意点

4.2 QWidget使用

4.3 QMainWindow使用

4.4 QDialog使用


1. QWidget类

1.1 构造方法

 QWidget(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())

1.2 public方法

bool acceptDrops() const
 QString accessibleDescription() const
 QString accessibleName() const
 QList actions() const
 void activateWindow()
 void addAction(QAction *action)
 QAction *addAction(const QString &text)
 QAction *addAction(const QIcon &icon, const QString &text)
 QAction *addAction(const QString &text, const QObject *receiver, const char *member, Qt::ConnectionType type = Qt::AutoConnection)
 QAction *addAction(const QIcon &icon, const QString &text, const QObject *receiver, const char *member, Qt::ConnectionType type = Qt::AutoConnection)
 QAction *addAction(const QString &text, Args &&... args)
 QAction *addAction(const QIcon &icon, const QString &text, Args &&... args)
 QAction *addAction(const QString &text, const QKeySequence &shortcut)
 QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut)
 QAction *addAction(const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char *member, Qt::ConnectionType type = Qt::AutoConnection)
 QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, const QObject *receiver, const char *member, Qt::ConnectionType type = Qt::AutoConnection)
 QAction *addAction(const QString &text, const QKeySequence &shortcut, Args &&... args)
 QAction *addAction(const QIcon &icon, const QString &text, const QKeySequence &shortcut, Args &&... args)
 void addActions(const QList &actions)
 void adjustSize()
 bool autoFillBackground() const
 QPalette::ColorRole backgroundRole() const
 QBackingStore *backingStore() const
 QSize baseSize() const
 QWidget *childAt(int x, int y) const
 QWidget *childAt(const QPoint &p) const
 QRect childrenRect() const
 QRegion childrenRegion() const
 void clearFocus()
 void clearMask()
 QMargins contentsMargins() const
 QRect contentsRect() const
 Qt::ContextMenuPolicy contextMenuPolicy() const
 QCursor cursor() const
 WId effectiveWinId() const
 void ensurePolished() const
 Qt::FocusPolicy focusPolicy() const
 QWidget *focusProxy() const
 QWidget *focusWidget() const
 const QFont &font() const
 QFontInfo fontInfo() const
 QFontMetrics fontMetrics() const
 QPalette::ColorRole foregroundRole() const
 QRect frameGeometry() const
 QSize frameSize() const
 const QRect &geometry() const
 QPixmap grab(const QRect &rectangle = QRect(QPoint(0, 0), QSize(-1, -1)))
 void grabGesture(Qt::GestureType gesture, Qt::GestureFlags flags = Qt::GestureFlags())
 void grabKeyboard()
 void grabMouse()
 void grabMouse(const QCursor &cursor)
 int grabShortcut(const QKeySequence &key, Qt::ShortcutContext context = Qt::WindowShortcut)
 QGraphicsEffect *graphicsEffect() const
 QGraphicsProxyWidget *graphicsProxyWidget() const
 bool hasEditFocus() const
 bool hasFocus() const
 virtual bool hasHeightForWidth() const
 bool hasMouseTracking() const
 bool hasTabletTracking() const
 int height() const
 virtual int heightForWidth(int w) const
 Qt::InputMethodHints inputMethodHints() const
 virtual QVariant inputMethodQuery(Qt::InputMethodQuery query) const
 void insertAction(QAction *before, QAction *action)
 void insertActions(QAction *before, const QList &actions)
 bool isActiveWindow() const
 bool isAncestorOf(const QWidget *child) const
 bool isEnabled() const
 bool isEnabledTo(const QWidget *ancestor) const
 bool isFullScreen() const
 bool isHidden() const
 bool isMaximized() const
 bool isMinimized() const
 bool isModal() const
 bool isVisible() const
 bool isVisibleTo(const QWidget *ancestor) const
 bool isWindow() const
 bool isWindowModified() const
 QLayout *layout() const
 Qt::LayoutDirection layoutDirection() const
 QLocale locale() const
 QPointF mapFrom(const QWidget *parent, const QPointF &pos) const
 QPoint mapFrom(const QWidget *parent, const QPoint &pos) const
 QPointF mapFromGlobal(const QPointF &pos) const
 QPoint mapFromGlobal(const QPoint &pos) const
 QPointF mapFromParent(const QPointF &pos) const
 QPoint mapFromParent(const QPoint &pos) const
 QPointF mapTo(const QWidget *parent, const QPointF &pos) const
 QPoint mapTo(const QWidget *parent, const QPoint &pos) const
 QPointF mapToGlobal(const QPointF &pos) const
 QPoint mapToGlobal(const QPoint &pos) const
 QPointF mapToParent(const QPointF &pos) const
 QPoint mapToParent(const QPoint &pos) const
 QRegion mask() const
 int maximumHeight() const
 QSize maximumSize() const
 int maximumWidth() const
 int minimumHeight() const
 QSize minimumSize() const
 virtual QSize minimumSizeHint() const
 int minimumWidth() const
 void move(const QPoint &)
 void move(int x, int y)
 QWidget *nativeParentWidget() const
 QWidget *nextInFocusChain() const
 QRect normalGeometry() const
 void overrideWindowFlags(Qt::WindowFlags flags)
 const QPalette &palette() const
 QWidget *parentWidget() const
 QPoint pos() const
 QWidget *previousInFocusChain() const
 QRect rect() const
 void releaseKeyboard()
 void releaseMouse()
 void releaseShortcut(int id)
 void removeAction(QAction *action)
 void render(QPaintDevice *target, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), QWidget::RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren))
 void render(QPainter *painter, const QPoint &targetOffset = QPoint(), const QRegion &sourceRegion = QRegion(), QWidget::RenderFlags renderFlags = RenderFlags(DrawWindowBackground | DrawChildren))
 void repaint(int x, int y, int w, int h)
 void repaint(const QRect &rect)
 void repaint(const QRegion &rgn)
 void resize(const QSize &)
 void resize(int w, int h)
 bool restoreGeometry(const QByteArray &geometry)
 QByteArray saveGeometry() const
 QScreen *screen() const
 void scroll(int dx, int dy)
 void scroll(int dx, int dy, const QRect &r)
 void setAcceptDrops(bool on)
 void setAccessibleDescription(const QString &description)
 void setAccessibleName(const QString &name)
 void setAttribute(Qt::WidgetAttribute attribute, bool on = true)
 void setAutoFillBackground(bool enabled)
 void setBackgroundRole(QPalette::ColorRole role)
 void setBaseSize(const QSize &)
 void setBaseSize(int basew, int baseh)
 void setContentsMargins(int left, int top, int right, int bottom)
 void setContentsMargins(const QMargins &margins)
 void setContextMenuPolicy(Qt::ContextMenuPolicy policy)
 void setCursor(const QCursor &)
 void setEditFocus(bool enable)
 void setFixedHeight(int h)
 void setFixedSize(const QSize &s)
 void setFixedSize(int w, int h)
 void setFixedWidth(int w)
 void setFocus(Qt::FocusReason reason)
 void setFocusPolicy(Qt::FocusPolicy policy)
 void setFocusProxy(QWidget *w)
 void setFont(const QFont &)
 void setForegroundRole(QPalette::ColorRole role)
 void setGeometry(const QRect &)
 void setGeometry(int x, int y, int w, int h)
 void setGraphicsEffect(QGraphicsEffect *effect)
 void setInputMethodHints(Qt::InputMethodHints hints)
 void setLayout(QLayout *layout)
 void setLayoutDirection(Qt::LayoutDirection direction)
 void setLocale(const QLocale &locale)
 void setMask(const QBitmap &bitmap)
 void setMask(const QRegion ®ion)
 void setMaximumHeight(int maxh)
 void setMaximumSize(const QSize &)
 void setMaximumSize(int maxw, int maxh)
 void setMaximumWidth(int maxw)
 void setMinimumHeight(int minh)
 void setMinimumSize(const QSize &)
 void setMinimumSize(int minw, int minh)
 void setMinimumWidth(int minw)
 void setMouseTracking(bool enable)
 void setPalette(const QPalette &)
 void setParent(QWidget *parent)
 void setParent(QWidget *parent, Qt::WindowFlags f)
 void setScreen(QScreen *screen)
 void setShortcutAutoRepeat(int id, bool enable = true)
 void setShortcutEnabled(int id, bool enable = true)
 void setSizeIncrement(const QSize &)
 void setSizeIncrement(int w, int h)
 void setSizePolicy(QSizePolicy)
 void setSizePolicy(QSizePolicy::Policy horizontal, QSizePolicy::Policy vertical)
 void setStatusTip(const QString &)
 void setStyle(QStyle *style)
 void setTabletTracking(bool enable)
 void setToolTip(const QString &)
 void setToolTipDuration(int msec)
 void setUpdatesEnabled(bool enable)
 void setWhatsThis(const QString &)
 void setWindowFilePath(const QString &filePath)
 void setWindowFlag(Qt::WindowType flag, bool on = true)
 void setWindowFlags(Qt::WindowFlags type)
 void setWindowIcon(const QIcon &icon)
 void setWindowModality(Qt::WindowModality windowModality)
 void setWindowOpacity(qreal level)
 void setWindowRole(const QString &role)
 void setWindowState(Qt::WindowStates windowState)
 void setupUi(QWidget *widget)
 QSize size() const
 virtual QSize sizeHint() const
 QSize sizeIncrement() const
 QSizePolicy sizePolicy() const
 void stackUnder(QWidget *w)
 QString statusTip() const
 QStyle *style() const
 QString styleSheet() const
 bool testAttribute(Qt::WidgetAttribute attribute) const
 QString toolTip() const
 int toolTipDuration() const
 bool underMouse() const
 void ungrabGesture(Qt::GestureType gesture)
 void unsetCursor()
 void unsetLayoutDirection()
 void unsetLocale()
 void update(int x, int y, int w, int h)
 void update(const QRect &rect)
 void update(const QRegion &rgn)
 void updateGeometry()
 bool updatesEnabled() const
 QRegion visibleRegion() const
 QString whatsThis() const
 int width() const
 WId winId() const
 QWidget *window() const
 QString windowFilePath() const
 Qt::WindowFlags windowFlags() const
 QWindow *windowHandle() const
 QIcon windowIcon() const
 Qt::WindowModality windowModality() const
 qreal windowOpacity() const
 QString windowRole() const
 Qt::WindowStates windowState() const
 QString windowTitle() const
 Qt::WindowType windowType() const
 int x() const
 int y() const

1.3 信号

 void customContextMenuRequested(const QPoint &pos)
 void windowIconChanged(const QIcon &icon)
 void windowTitleChanged(const QString &title)

1.4 槽

 bool close()
 void hide()
 void lower()
 void raise()
 void repaint()
 void setDisabled(bool disable)
 void setEnabled(bool)
 void setFocus()
 void setHidden(bool hidden)
 void setStyleSheet(const QString &styleSheet)
 virtual void setVisible(bool visible)
 void setWindowModified(bool)
 void setWindowTitle(const QString &)
 void show()
 void showFullScreen()
 void showMaximized()
 void showMinimized()
 void showNormal()
 void update()

2. QMainWindow类

2.1 构造方法

QMainWindow(QWidget *parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags())

2.2 public方法

void addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget)
 void addDockWidget(Qt::DockWidgetArea area, QDockWidget *dockwidget, Qt::Orientation orientation)
 void addToolBar(Qt::ToolBarArea area, QToolBar *toolbar)
 void addToolBar(QToolBar *toolbar)
 QToolBar *addToolBar(const QString &title)
 void addToolBarBreak(Qt::ToolBarArea area = Qt::TopToolBarArea)
 QWidget *centralWidget() const
 Qt::DockWidgetArea corner(Qt::Corner corner) const
 virtual QMenu *createPopupMenu()
 QMainWindow::DockOptions dockOptions() const
 Qt::DockWidgetArea dockWidgetArea(QDockWidget *dockwidget) const
 bool documentMode() const
 QSize iconSize() const
 void insertToolBar(QToolBar *before, QToolBar *toolbar)
 void insertToolBarBreak(QToolBar *before)
 bool isAnimated() const
 bool isDockNestingEnabled() const
 QMenuBar *menuBar() const
 QWidget *menuWidget() const
 void removeDockWidget(QDockWidget *dockwidget)
 void removeToolBar(QToolBar *toolbar)
 void removeToolBarBreak(QToolBar *before)
 void resizeDocks(const QList &docks, const QList &sizes, Qt::Orientation orientation)
 bool restoreDockWidget(QDockWidget *dockwidget)
 bool restoreState(const QByteArray &state, int version = 0)
 QByteArray saveState(int version = 0) const
 void setCentralWidget(QWidget *widget)
 void setCorner(Qt::Corner corner, Qt::DockWidgetArea area)
 void setDockOptions(QMainWindow::DockOptions options)
 void setDocumentMode(bool enabled)
 void setIconSize(const QSize &iconSize)
 void setMenuBar(QMenuBar *menuBar)
 void setMenuWidget(QWidget *menuBar)
 void setStatusBar(QStatusBar *statusbar)
 void setTabPosition(Qt::DockWidgetAreas areas, QTabWidget::TabPosition tabPosition)
 void setTabShape(QTabWidget::TabShape tabShape)
 void setToolButtonStyle(Qt::ToolButtonStyle toolButtonStyle)
 void splitDockWidget(QDockWidget *first, QDockWidget *second, Qt::Orientation orientation)
 QStatusBar *statusBar() const
 QTabWidget::TabPosition tabPosition(Qt::DockWidgetArea area) const
 QTabWidget::TabShape tabShape() const
 QList tabifiedDockWidgets(QDockWidget *dockwidget) const
 void tabifyDockWidget(QDockWidget *first, QDockWidget *second)
 QWidget *takeCentralWidget()
 Qt::ToolBarArea toolBarArea(const QToolBar *toolbar) const
 bool toolBarBreak(QToolBar *toolbar) const
 Qt::ToolButtonStyle toolButtonStyle() const
 bool unifiedTitleAndToolBarOnMac() const

2.3 信号

 void iconSizeChanged(const QSize &iconSize)
 void tabifiedDockWidgetActivated(QDockWidget *dockWidget)
 void toolButtonStyleChanged(Qt::ToolButtonStyle toolButtonStyle)

2.4 槽

 void setAnimated(bool enabled)
 void setDockNestingEnabled(bool enabled)
 void setUnifiedTitleAndToolBarOnMac(bool set)

3. QDialog类

3.1 构造方法

 QDialog(QWidget *parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags())

3.2 public方法

 bool isSizeGripEnabled() const
 int result() const
 void setModal(bool modal)
 void setResult(int i)
 void setSizeGripEnabled(bool)

3.3 信号

 void accepted()
 void finished(int result)
 void rejected()

3.4 槽

 virtual void accept()
 virtual void done(int r)
 virtual int exec()
 virtual void open()
 virtual void reject()

4.使用举例

4.1 Qt Creator使用注意点

(1)QT designer界面介绍

QT系列第1节 QT中窗口使用简介_第2张图片

 (2)布局管理

水平布局

垂直布局

网格布局

表格布局:和网格布局类似,一般使用网格布局就够了

打破布局:

QT系列第1节 QT中窗口使用简介_第3张图片

实际业务开发中,主窗口是要设置布局的,不然某些控件可能不能显示,或者改变窗口控件大小不能自适应,这一点在开发中要注意

 (3)修改程序左上角图标

QT系列第1节 QT中窗口使用简介_第4张图片

 (4)UI设计方式

UI设计可以使用designer设计,也可以通过编写代码设计,有些UI设计designer实现不了,所有的UI都可以通过代码来实现,在实际开发中,一般以两种结合实现UI效果。

(5)UI designer转到槽

鼠标选中控件,右键菜单“转到槽”,选择槽函数,就会进入代码编写页面

QT系列第1节 QT中窗口使用简介_第5张图片

 QT系列第1节 QT中窗口使用简介_第6张图片

 QT系列第1节 QT中窗口使用简介_第7张图片

 (5)显示console并在其中输出qDebug信息

步骤一:.pro文件中增加: CONFIG += console

步骤二:如下选项打勾

QT系列第1节 QT中窗口使用简介_第8张图片

4.2 QWidget使用

QT系列第1节 QT中窗口使用简介_第9张图片

 如下程序演示点击按钮获取输入框信息,并弹出消息框显示输出内容,运行效果如下:

QT系列第1节 QT中窗口使用简介_第10张图片

代码:

工程文件:

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++17 console

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    widget.cpp

HEADERS += \
    widget.h

FORMS += \
    widget.ui

RC_ICONS = MainIcon.ico

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

 窗口头文件,窗口实现文件,主调文件内容如下:

#ifndef WIDGET_H
#define WIDGET_H

#include 

QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

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

private slots:
    void on_pushButtonGet_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H
#include "widget.h"
#include "ui_widget.h"
#include 
#include 

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

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButtonGet_clicked()
{
    QString strInput = ui->lineEditInput->text();
    qDebug() << strInput;
    QMessageBox::information(this, "提示", strInput);
}

#include "widget.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

4.3 QMainWindow使用

QT系列第1节 QT中窗口使用简介_第11张图片

如下程序演示继承QMainWindow类,不用Qt designer通过手写代码的方式添加布局和控件,效果展示如下:
QT系列第1节 QT中窗口使用简介_第12张图片

代码如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    void initUI();
    void initSignalSlot();

private slots:
    void on_under_line(bool sel);
    void on_italic(bool sel);
    void on_bold(bool sel);

    void set_text_font_color();

private:
    QCheckBox * m_checkBoxUnderLine;
    QCheckBox * m_checkBoxItalic;
    QCheckBox * m_checkBoxBold;

    QRadioButton * m_rBtnBlack;
    QRadioButton * m_rBtnBlue;
    QRadioButton * m_rBtnRed;

    QPlainTextEdit * m_palinTextEdit;

    QPushButton * m_pushBtnOK;
    QPushButton * m_pushBtnCancel;
    QPushButton * m_pushBtnClose;

};
#endif // MAINWINDOW_H
#include "mainwindow.h"
#include 
#include 
#include 

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    initUI();
    initSignalSlot();
}

void MainWindow::initUI()
{
    /*
    if (this->objectName().isEmpty())
    {
        this->setObjectName("MainWindow");
        this->resize(555, 412);
    }
    */

    m_checkBoxUnderLine = new QCheckBox(tr("UnderLine"));
    m_checkBoxItalic = new QCheckBox(tr("Italic"));
    m_checkBoxBold = new QCheckBox(tr("Bold"));
    QHBoxLayout *hLayout1 = new QHBoxLayout;
    hLayout1->addWidget(m_checkBoxUnderLine);
    hLayout1->addWidget(m_checkBoxItalic);
    hLayout1->addWidget(m_checkBoxBold);

    m_rBtnBlack = new QRadioButton(tr("Black"));
    m_rBtnBlue = new QRadioButton(tr("Blue"));
    m_rBtnRed = new QRadioButton(tr("Red"));
    QHBoxLayout *hLayout2 = new QHBoxLayout;
    hLayout2->addWidget(m_rBtnBlack);
    hLayout2->addWidget(m_rBtnBlue);
    hLayout2->addWidget(m_rBtnRed);

    m_palinTextEdit = new QPlainTextEdit(tr(""));
    QFont font = m_palinTextEdit->font();
    font.setPointSize(20);
    m_palinTextEdit->setFont(font);
    m_palinTextEdit->setPlainText("QT\n\n你好啊\n\n很高兴认识你!");

    m_pushBtnOK = new QPushButton(tr("Ok"));
    m_pushBtnCancel = new QPushButton(tr("Cancel"));
    m_pushBtnClose = new QPushButton(tr("Close"));
    QHBoxLayout *hLayout3 = new QHBoxLayout;
    hLayout3->addStretch();
    hLayout3->addWidget(m_pushBtnOK);
    hLayout3->addWidget(m_pushBtnCancel);
    hLayout3->addStretch();
    hLayout3->addWidget(m_pushBtnClose);

    QVBoxLayout *vLayout = new QVBoxLayout;
    vLayout->addLayout(hLayout1);
    vLayout->addLayout(hLayout2);
    vLayout->addWidget(m_palinTextEdit);
    vLayout->addLayout(hLayout3);

    //说明直接调用setLayout窗口显示不出控件,原因是MainWindow已经
    //有默认的layout了,此处我们新建一个QWidget,然后让MainWindow
    //将新建的QWidget来安装,然后在新建的QWidget中进行相关布局
    //setLayout(vLayout);
    QWidget *widget = new QWidget(this);
    widget->setLayout(vLayout);
    setCentralWidget(widget);
}

void MainWindow::initSignalSlot()
{
    connect(m_checkBoxUnderLine, SIGNAL(clicked(bool)), this, SLOT(on_under_line(bool)));
    connect(m_checkBoxItalic, SIGNAL(clicked(bool)), this, SLOT(on_italic(bool)));
    connect(m_checkBoxBold, SIGNAL(clicked(bool)), this, SLOT(on_bold(bool)));

    connect(m_rBtnBlack, SIGNAL(clicked()), this, SLOT(set_text_font_color()));
    connect(m_rBtnBlue, SIGNAL(clicked()), this, SLOT(set_text_font_color()));
    connect(m_rBtnRed, SIGNAL(clicked()), this, SLOT(set_text_font_color()));

    connect(m_pushBtnOK, SIGNAL(clicked()), this, SLOT(accept()));
    connect(m_pushBtnCancel, SIGNAL(clicked()), this, SLOT(reject()));
    connect(m_pushBtnClose, SIGNAL(clicked()), this, SLOT(close()));
}

void MainWindow::on_under_line(bool sel)
{
    QFont font = m_palinTextEdit->font();
    font.setUnderline(sel);
    m_palinTextEdit->setFont(font);
}

void MainWindow::on_italic(bool sel)
{
    QFont font = m_palinTextEdit->font();
    font.setItalic(sel);
    m_palinTextEdit->setFont(font);
}

void MainWindow::on_bold(bool sel)
{
    QFont font = m_palinTextEdit->font();
    font.setBold(sel);
    m_palinTextEdit->setFont(font);
}

void MainWindow::set_text_font_color()
{
    QPalette plet = m_palinTextEdit->palette();
    if (m_rBtnBlack->isChecked()) {
        plet.setColor(QPalette::Text, Qt::black);
    }
    else if (m_rBtnBlue->isChecked()) {
        plet.setColor(QPalette::Text, Qt::blue);
    }
    else {
        plet.setColor(QPalette::Text, Qt::red);
    }
    m_palinTextEdit->setPalette(plet);
}

MainWindow::~MainWindow()
{
}

#include "mainwindow.h"

#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

4.4 QDialog使用

QT系列第1节 QT中窗口使用简介_第13张图片

如下程序演示使用QDialog制作一个大小固定的登录窗口,效果显示如下:

QT系列第1节 QT中窗口使用简介_第14张图片

 界面布局如下:

QT系列第1节 QT中窗口使用简介_第15张图片

代码如下:

#ifndef LOGIN_H
#define LOGIN_H

#include 

namespace Ui {
class Login;
}

class Login : public QDialog
{
    Q_OBJECT

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

private:
    Ui::Login *ui;
};

#endif // LOGIN_H
#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    setFixedSize(399, 318);
}

Login::~Login()
{
    delete ui;
}
#include "login.h"
#include "ui_login.h"

Login::Login(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Login)
{
    ui->setupUi(this);
    setFixedSize(399, 318);
}

Login::~Login()
{
    delete ui;
}

你可能感兴趣的:(QT,开发语言,QT)