Qt實(shí)現(xiàn)UI界面純代碼示例
1.相關(guān)信息
設(shè)置編輯框內(nèi)容的字體樣式,包括加粗、下劃線、斜體、藍(lán)色、紅色、黑色
2.界面展示

3.相關(guān)代碼
#include "dialog.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QPlainTextEdit>
#include <QPushButton>
// 下劃線
void Dialog::do_chkBoxUnder(bool checked)
{
QFont font = txtEdit->font();
font.setUnderline(checked);
txtEdit->setFont(font);
}
// 斜體
void Dialog::do_chkBoxItalic(bool checked)
{
QFont font = txtEdit->font();
font.setItalic(checked);
txtEdit->setFont(font);
}
// 加粗
void Dialog::do_chkBoxBold(bool checked)
{
QFont font = txtEdit->font();
font.setBold(checked);
txtEdit->setFont(font);
}
// 設(shè)置顏色
void Dialog::do_setFontColor()
{
QPalette plet = txtEdit->palette();
if(radioRed->isChecked()){
plet.setColor(QPalette::Text, Qt::red);
}else if(radioBlack->isChecked()){
plet.setColor(QPalette::Text, Qt::black);
}else if(radioBlue->isChecked()){
plet.setColor(QPalette::Text, Qt::blue);
}
txtEdit->setPalette(plet);
}
Dialog::Dialog(QWidget *parent)
: QDialog(parent)
{
// 豎布局:字體樣式
chkBoxUnder = new QCheckBox("下劃線");
chkBoxItalic = new QCheckBox("斜體");
chkBoxBold = new QCheckBox("加粗");
QHBoxLayout *HLay1 = new QHBoxLayout();
HLay1->addWidget(chkBoxUnder);
HLay1->addWidget(chkBoxItalic);
HLay1->addWidget(chkBoxBold);
// 豎布局:字體顏色
radioBlack = new QRadioButton("黑色");
radioRed = new QRadioButton("紅色");
radioBlue = new QRadioButton("藍(lán)色");
QHBoxLayout *HLay2 = new QHBoxLayout();
HLay2->addWidget(radioBlack);
HLay2->addWidget(radioRed);
HLay2->addWidget(radioBlue);
// 編輯框
txtEdit = new QPlainTextEdit();
txtEdit->setPlainText("hello world \n 手工創(chuàng)建");
QFont font = txtEdit->font();
font.setPointSize(20); // 字體大小
txtEdit->setFont(font);
// 確認(rèn)、取消、退出
btnOk = new QPushButton("確定");
btnCancel = new QPushButton("取消");
btnClose = new QPushButton("退出");
QHBoxLayout *HLay3 = new QHBoxLayout();
HLay3->addStretch();
HLay3->addWidget(btnOk);
HLay3->addWidget(btnCancel);
HLay3->addStretch();
HLay3->addWidget(btnClose);
QVBoxLayout *VLay = new QVBoxLayout();
VLay->addLayout(HLay1);
VLay->addLayout(HLay2);
VLay->addWidget(txtEdit);
VLay->addLayout(HLay3);
setLayout(VLay);
// 信號與槽
connect(chkBoxUnder, SIGNAL(clicked(bool)), this, SLOT(do_chkBoxUnder(bool)));
connect(chkBoxItalic, SIGNAL(clicked(bool)), this, SLOT(do_chkBoxItalic(bool)));
connect(chkBoxBold, SIGNAL(clicked(bool)), this, SLOT(do_chkBoxBold(bool)));
connect(radioRed, SIGNAL(clicked()), this, SLOT(do_setFontColor()));
connect(radioBlack, SIGNAL(clicked()), this, SLOT(do_setFontColor()));
connect(radioBlue, SIGNAL(clicked()), this, SLOT(do_setFontColor()));
connect(btnOk, SIGNAL(clicked()), this, SLOT(accept()));
connect(btnCancel, SIGNAL(clicked()), this, SLOT(reject()));
connect(btnClose, SIGNAL(clicked()), this, SLOT(close()));
setWindowTitle("手工打造UI");
}
Dialog::~Dialog() {}總結(jié)
到此這篇關(guān)于Qt實(shí)現(xiàn)UI界面的文章就介紹到這了,更多相關(guān)Qt實(shí)現(xiàn)UI界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?primer超詳細(xì)講解關(guān)聯(lián)容器
兩個(gè)主要的關(guān)聯(lián)容器為map和set,map中元素是一些關(guān)鍵字—值對,關(guān)鍵字起索引的作用,值則表示與索引相關(guān)聯(lián)的數(shù)據(jù)。set中每個(gè)元素只包含一個(gè)關(guān)鍵字,set支持高效的關(guān)鍵字查詢操作——檢查一個(gè)給定關(guān)鍵字是否在set中2022-07-07
C語言棧的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語言棧的表示與實(shí)現(xiàn),對于數(shù)據(jù)結(jié)構(gòu)與算法的研究有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07
c++ 虛函數(shù)與純虛函數(shù)的區(qū)別(深入分析)
本篇文章是對c++中虛函數(shù)與純虛函數(shù)的區(qū)別進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)LeetCode(112.二叉樹的路徑和)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(112.二叉樹的路徑和),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
解析c++中參數(shù)對象與局部對象的析構(gòu)順序的詳解
本篇文章是對c++中參數(shù)對象與局部對象的析構(gòu)順序進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C++實(shí)現(xiàn)轉(zhuǎn)置矩陣的循環(huán)
大家好,本篇文章主要講的是C++實(shí)現(xiàn)轉(zhuǎn)置矩陣的循環(huán),感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01
C++中設(shè)計(jì)一個(gè)類時(shí)的注意事項(xiàng)分享
這篇文章主要來和大家分享一下C++中,設(shè)計(jì)一個(gè)類要注意哪些東西,這往往也是C++面試時(shí)會考到的問題,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-06-06

