日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

C/C++?Qt?StatusBar底部狀態(tài)欄應(yīng)用教程_C 語言

更新時(shí)間: 2021-11-12 編程語言

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 &lt;QLabel&gt;

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    // 隱藏狀態(tài)欄下方三角形
    ui-&gt;statusBar-&gt;setSizeGripEnabled(false);

    // 新增標(biāo)簽欄
    QLabel *label_url = new QLabel(this);
    QLabel *label_about = new QLabel(this);

    // 配置連接
    label_url-&gt;setFrameStyle(QFrame::Box | QFrame::Sunken);
    label_url-&gt;setText(tr("&lt;a href=\"https://lyshark.cnblogs.com\"&gt;訪問主頁&lt;/a&gt;"));
    label_url-&gt;setOpenExternalLinks(true);

    label_about-&gt;setFrameStyle(QFrame::Box | QFrame::Sunken);
    label_about-&gt;setText(tr("&lt;a href=\"https://lyshark.cnblogs.com\"&gt;關(guān)于我&lt;/a&gt;"));
    label_about-&gt;setOpenExternalLinks(true);

    // 將信息增加到底部(永久添加)
    ui-&gt;statusBar-&gt;addPermanentWidget(label_url);
    ui-&gt;statusBar-&gt;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

欄目分類
最近更新