網(wǎng)站首頁 編程語言 正文
Qt窗體中默認(rèn)會(huì)附加一個(gè)QstatusBar組件,狀態(tài)欄組件位于主窗體的最下方,其作用是提供一個(gè)工具提示功能,當(dāng)程序中有提示信息是可以動(dòng)態(tài)的顯示在這個(gè)區(qū)域內(nèi),狀態(tài)欄組件內(nèi)可以增加任何Qt中的通用組件,只需要通過addWidget函數(shù)動(dòng)態(tài)追加即可引入到底部,底部狀態(tài)欄在實(shí)際開發(fā)中應(yīng)用非常普遍,以下代碼是對(duì)該組件基本使用方法的總結(jié)。
首先我們通過new新增3個(gè)QLabel組件,并將該組件依次排列在底部狀態(tài)欄內(nèi),實(shí)現(xiàn)代碼如下所示:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 初始化狀態(tài)欄 QLabel *labCellIndex = new QLabel("當(dāng)前坐標(biāo): 0.0",this); labCellIndex->setMinimumWidth(250); QLabel *labCellType=new QLabel("單元格類型: null",this); labCellType->setMinimumWidth(200); QLabel *labStudID=new QLabel("學(xué)生ID: 0",this); labStudID->setMinimumWidth(200); // 將初始化的標(biāo)簽添加到底部狀態(tài)欄上 ui->statusBar->addWidget(labCellIndex); ui->statusBar->addWidget(labCellType); ui->statusBar->addWidget(labStudID); } MainWindow::~MainWindow() { delete ui; }
運(yùn)行代碼效果如下:
QLabel組件除了可以增加提示信息以外,通過設(shè)置setOpenExternalLinks可以將這個(gè)組件設(shè)置為以鏈接形式出現(xiàn),有利于我們?cè)黾泳W(wǎng)頁跳轉(zhuǎn)等功能。
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // 隱藏狀態(tài)欄下方三角形 ui->statusBar->setSizeGripEnabled(false); // 新增標(biāo)簽欄 QLabel *label_url = new QLabel(this); QLabel *label_about = new QLabel(this); // 配置連接 label_url->setFrameStyle(QFrame::Box | QFrame::Sunken); label_url->setText(tr("<a href=\"https://lyshark.cnblogs.com\">訪問主頁</a>")); label_url->setOpenExternalLinks(true); label_about->setFrameStyle(QFrame::Box | QFrame::Sunken); label_about->setText(tr("<a href=\"https://lyshark.cnblogs.com\">關(guān)于我</a>")); label_about->setOpenExternalLinks(true); // 將信息增加到底部(永久添加) ui->statusBar->addPermanentWidget(label_url); ui->statusBar->addPermanentWidget(label_about); } MainWindow::~MainWindow() { delete ui; }
運(yùn)行代碼效果如下:
同理,只要是通用組件都可以被安置到底部菜單欄,如果我們需要增加進(jìn)度條組件只需要這樣寫:
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> #include <QProgressBar> QProgressBar *pro; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); pro = new QProgressBar(this); // 自動(dòng)計(jì)算 ui->statusBar->addPermanentWidget(pro, 1); // 設(shè)置進(jìn)度是否顯示 pro->setTextVisible(true); // 設(shè)置初始化進(jìn)度位置 pro->setValue(0); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_pushButton_clicked() { qint32 count = pro->value(); count = count +10; pro->setValue(count); }
運(yùn)行代碼效果如下:
接著我們?cè)黾右粋€(gè)tablewidget并初始化參數(shù),tableWidget組件存在一個(gè)on_tableWidget_currentCellChanged屬性,該屬性的作用是,只要Table表格存在變化則會(huì)觸發(fā),當(dāng)用戶選擇不同的表格,我們可以將當(dāng)前表格行列自動(dòng)設(shè)置到狀態(tài)欄中,從而實(shí)現(xiàn)同步狀態(tài)欄消息提示,起到時(shí)刻動(dòng)態(tài)顯示的作用。
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QLabel> #include <QTableWidget> #include <QTableWidgetItem> QLabel *labCellIndex; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // ------------------------------------------------------------------------------------ // 初始化狀態(tài)欄 labCellIndex = new QLabel("當(dāng)前坐標(biāo): 0.0",this); labCellIndex->setMinimumWidth(250); // 將初始化的標(biāo)簽添加到底部狀態(tài)欄上 ui->statusBar->addWidget(labCellIndex); // ------------------------------------------------------------------------------------ // 填充數(shù)據(jù),對(duì)表格進(jìn)行初始化操作 QStringList header; header << "姓名" << "性別" << "年齡"; ui->tableWidget->setColumnCount(header.size()); // 設(shè)置表格的列數(shù) ui->tableWidget->setHorizontalHeaderLabels(header); // 設(shè)置水平頭 ui->tableWidget->setRowCount(5); // 設(shè)置總行數(shù) ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); // 設(shè)置表結(jié)構(gòu)默認(rèn)不可編輯 // 填充數(shù)據(jù) QStringList NameList; NameList << "lyshark A" << "lyshark B" << "lyshark C"; QStringList SexList; SexList << "男" << "男" << "女"; qint32 AgeList[3] = {22,23,43}; // 針對(duì)獲取元素使用 NameList[x] 和使用 NameList.at(x)效果相同 for(int x=0;x< 3;x++) { int col =0; // 添加姓名 ui->tableWidget->setItem(x,col++,new QTableWidgetItem(NameList[x])); // 添加性別 ui->tableWidget->setItem(x,col++,new QTableWidgetItem(SexList.at(x))); // 添加年齡 ui->tableWidget->setItem(x,col++,new QTableWidgetItem( QString::number(AgeList[x]) ) ); } } // 當(dāng)前選擇單元格發(fā)生變化時(shí)觸發(fā)響應(yīng)事件,也就是將底部狀態(tài)欄標(biāo)簽設(shè)置 // https://www.cnblogs.com/lyshark void MainWindow::on_tableWidget_currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) { Q_UNUSED(previousRow); Q_UNUSED(previousColumn); // 顯示行與列的變化數(shù)值 //std::cout << "currentRow = " << currentRow << " currentColumn = " << currentColumn << std::endl; //std::cout << "pre Row = " << previousRow << " pre Column = " << previousColumn << std::endl; // 獲取當(dāng)前單元格的Item QTableWidgetItem *item = ui->tableWidget->item(currentRow,currentColumn); if(item == NULL) return; // 設(shè)置單元格坐標(biāo) labCellIndex->setText(QString::asprintf("當(dāng)前坐標(biāo): %d 行 | %d 列",currentRow,currentColumn)); } MainWindow::~MainWindow() { delete ui; }
運(yùn)行代碼效果如下:
原文鏈接:https://www.cnblogs.com/LyShark/p/15637184.html
相關(guān)推薦
- 2023-03-16 python內(nèi)置函數(shù)anext的具體使用_python
- 2022-09-29 Python組合數(shù)據(jù)類型詳解_python
- 2022-07-21 css讓不定寬高的div,垂直水平居中
- 2022-12-09 C++輸出問題:保留兩位小數(shù)_C 語言
- 2023-03-23 Pandas分組聚合之使用自定義函數(shù)方法transform()、apply()_python
- 2022-02-22 解決:DevTools failed to load SourceMap:... net::ERR_
- 2023-09-18 Echarts常見問題總結(jié)(持續(xù)更新)
- 2022-09-20 Springboot整合Redis與數(shù)據(jù)持久化_Redis
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支