Qt實現(xiàn)UI界面純代碼示例
更新時間:2024年01月23日 15:39:29 作者:木木夕木目心.HDS
這篇文章主要給大家介紹了關(guān)于Qt實現(xiàn)UI界面的相關(guān)資料,使用Qt純代碼,實現(xiàn)了基本的界面,對大家學習或者使用Qt具有一定的參考借鑒價值,需要的朋友可以參考下
1.相關(guān)信息
設置編輯框內(nèi)容的字體樣式,包括加粗、下劃線、斜體、藍色、紅色、黑色
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); } // 設置顏色 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("藍色"); 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); // 確認、取消、退出 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實現(xiàn)UI界面的文章就介紹到這了,更多相關(guān)Qt實現(xiàn)UI界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
C++?primer超詳細講解關(guān)聯(lián)容器
兩個主要的關(guān)聯(lián)容器為map和set,map中元素是一些關(guān)鍵字—值對,關(guān)鍵字起索引的作用,值則表示與索引相關(guān)聯(lián)的數(shù)據(jù)。set中每個元素只包含一個關(guān)鍵字,set支持高效的關(guān)鍵字查詢操作——檢查一個給定關(guān)鍵字是否在set中2022-07-07c++ 虛函數(shù)與純虛函數(shù)的區(qū)別(深入分析)
本篇文章是對c++中虛函數(shù)與純虛函數(shù)的區(qū)別進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++實現(xiàn)LeetCode(112.二叉樹的路徑和)
這篇文章主要介紹了C++實現(xiàn)LeetCode(112.二叉樹的路徑和),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07解析c++中參數(shù)對象與局部對象的析構(gòu)順序的詳解
本篇文章是對c++中參數(shù)對象與局部對象的析構(gòu)順序進行了詳細的分析介紹,需要的朋友參考下2013-05-05C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán)
大家好,本篇文章主要講的是C++實現(xiàn)轉(zhuǎn)置矩陣的循環(huán),感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽2022-01-01