2024-09-08第二波的二進制讀寫

1.



3.



03



.



44


創(chuàng)建的 表格:



03


void on_actTabReset_triggered(); //表格復位




void MainWindow::resetTable(int aRowCount){ //表格復位,先刪除所有行,再設置新的行數,表頭不變// QStringList headerList;// headerList<<"測深(m)"<<"垂深(m)"<<"方位(°)"<<"總位移(m)"<<"固井質量"<<"測井取樣";// theModel->setHorizontalHeaderLabels(headerList); //設置表頭文字 theModel->removeRows(0,theModel->rowCount()); //刪除所有行 theModel->setRowCount(aRowCount);//設置新的行數 QString str=theModel->headerData(theModel->columnCount()-1, Qt::Horizontal,Qt::DisplayRole).toString(); for (int i=0;i<theModel->rowCount();i++) { //設置最后一列 QModelIndex index=theModel->index(i,FixedColumnCount-1); //獲取模型索引 QStandardItem* aItem=theModel->itemFromIndex(index); //獲取item aItem->setCheckable(true); aItem->setData(str,Qt::DisplayRole); aItem->setEditable(false); //不可編輯 }}


void MainWindow::resetTable(int aRowCount)

{ //表格復位,先刪除所有行,再設置新的行數,表頭不變

//? ? QStringList? ? headerList;

//? ? headerList<<"測深(m)"<<"垂深(m)"<<"方位(°)"<<"總位移(m)"<<"固井質量"<<"測井取樣";

//? ? theModel->setHorizontalHeaderLabels(headerList); //設置表頭文字

? ? theModel->removeRows(0,theModel->rowCount()); //刪除所有行

? ? theModel->setRowCount(aRowCount);//設置新的行數

? ? QString str=theModel->headerData(theModel->columnCount()-1,

? ? ? ? ? ? ? ? ? ? Qt::Horizontal,Qt::DisplayRole).toString();

? ? for (int i=0;irowCount();i++)

? ? { //設置最后一列

? ? ? ? QModelIndex index=theModel->index(i,FixedColumnCount-1); //獲取模型索引

? ? ? ? QStandardItem* aItem=theModel->itemFromIndex(index); //獲取item

? ? ? ? aItem->setCheckable(true);

? ? ? ? aItem->setData(str,Qt::DisplayRole);

? ? ? ? aItem->setEditable(false); //不可編輯

? ? }

}




下一個hasn't函數:


02


void MainWindow::on_actOpen_triggered()

{

? ? QString curPath=QDir::currentPath();

//調用打開文件對話框打開一個文件

? ? QString aFileName=QFileDialog::getOpenFileName(this,tr("打開一個文件"),curPath,

? ? ? ? ? ? ? ? "流數據文件(*.stm)");

? ? if (aFileName.isEmpty())

? ? ? ? return; //

? ? if? (openDataAsStream(aFileName)) //保存為流數據文件

? ? ? ? QMessageBox::information(this,"提示消息","文件已經打開!");

}




分析:

你的代碼是一個槽函數 `on_actOpen_triggered()` 的實現,用于響應用戶打開文件的操作。下面是對這段代碼的詳細分析:

### 代碼功能概述

1. **獲取當前工作目錄:**

? ```cpp

? QString curPath = QDir::currentPath();

? ```

? 這行代碼獲取了當前的工作目錄,并將其存儲在 `curPath` 變量中。這個目錄將作為文件對話框的默認路徑。

2. **打開文件對話框:**

? ```cpp

? QString aFileName = QFileDialog::getOpenFileName(

? ? ? this, tr("打開一個文件"), curPath, "流數據文件(*.stm)"

? );

? ```

? 這行代碼彈出一個文件對話框,允許用戶選擇一個文件。`getOpenFileName` 函數的參數分別是:

? - `this`:當前窗口的指針。

? - `tr("打開一個文件")`:對話框的標題。

? - `curPath`:對話框初始顯示的路徑。

? - `"流數據文件(*.stm)"`:文件過濾器,只顯示擴展名為 `.stm` 的文件。

? 函數返回選擇的文件的路徑,如果用戶取消了對話框,`aFileName` 將為空。

3. **檢查文件名是否為空:**

? ```cpp

? if (aFileName.isEmpty())

? ? ? return;

? ```

? 如果用戶取消了文件選擇對話框,`aFileName` 會為空,代碼會直接返回,不繼續(xù)執(zhí)行后續(xù)操作。

4. **打開文件并顯示提示消息:**

? ```cpp

? if (openDataAsStream(aFileName))

? ? ? QMessageBox::information(this, "提示消息", "文件已經打開!");

? ```

? `openDataAsStream(aFileName)` 是一個自定義函數(假設它是一個成員函數或在其他地方定義的函數),用于打開指定的文件。如果文件成功打開,函數返回 `true`,然后顯示一個信息框,告知用戶文件已經成功打開。

### 代碼改進建議

1. **錯誤處理**:

? - 目前代碼只在文件成功打開時顯示消息框,但沒有處理文件打開失敗的情況。建議添加文件打開失敗的錯誤處理代碼,例如:

? ? ```cpp

? ? if (openDataAsStream(aFileName)) {

? ? ? ? QMessageBox::information(this, "提示消息", "文件已經打開!");

? ? } else {

? ? ? ? QMessageBox::warning(this, "打開文件失敗", "無法打開選定的文件。");

? ? }

? ? ```

2. **改進文件過濾器**:

? - 文件過濾器 `"流數據文件(*.stm)"` 可以考慮加入更多的文件類型,或者將過濾器提取到常量中以便維護。例如:

? ? ```cpp

? ? const QString filter = "流數據文件(*.stm);;所有文件(*.*)";

? ? QString aFileName = QFileDialog::getOpenFileName(this, tr("打開一個文件"), curPath, filter);

? ? ```

3. **確保 `openDataAsStream` 的正確實現**:

? - 代碼中假設 `openDataAsStream` 函數已經實現并正確工作。如果 `openDataAsStream` 有可能拋出異?;蚍祷劐e誤,確保它的實現是健壯的并且可以正確處理各種情況。

4. **本地化支持**:

? - 如果你的應用需要多語言支持,確保使用 `tr()` 進行字符串本地化。你已經在對話框標題中使用了 `tr()`,確保所有用戶界面字符串都使用 `tr()` 進行本地化,以便在不同語言環(huán)境下能正確顯示。

通過這些改進,可以使你的代碼更加健壯、靈活且用戶友好。



不懂就問:



bool MainWindow::openDataAsStream(QString &aFileName)

{ //從Qt預定義流文件讀入數據

? ? QFile aFile(aFileName);? //以文件方式讀出

? ? if (!(aFile.open(QIODevice::ReadOnly)))

? ? ? ? return false;

? ? QDataStream aStream(&aFile); //用文本流讀取文件

? ? aStream.setVersion(QDataStream::Qt_5_9); //設置流文件版本號

? ? qint16? rowCount,colCount;

? ? aStream>>rowCount; //讀取行數

? ? aStream>>colCount; //列數




完整的 :


bool MainWindow::openDataAsStream(QString &aFileName)

{ //從Qt預定義流文件讀入數據

? ? QFile aFile(aFileName);? //以文件方式讀出

? ? if (!(aFile.open(QIODevice::ReadOnly)))

? ? ? ? return false;


? ? QDataStream aStream(&aFile); //用文本流讀取文件

? ? aStream.setVersion(QDataStream::Qt_5_9); //設置流文件版本號


? ? qint16? rowCount,colCount;

? ? aStream>>rowCount; //讀取行數

? ? aStream>>colCount; //列數


? ? this->resetTable(rowCount); //表格復位


? ? //獲取表頭文字

? ? QString str;

? ? for (int i=0;i<colCount;i++)

? ? ? ? aStream>>str;? //讀取表頭字符串


? ? //獲取數據區(qū)文字,

? ? qint16? ceShen;

? ? qreal? chuiShen;

? ? qreal? fangWei;

? ? qreal? weiYi;

? ? QString? zhiLiang;

? ? bool? ? quYang;

? ? QStandardItem? ?*aItem;

? ? QModelIndex index;


? ? for (int i=0;i<rowCount;i++)

? ? {

? ? ? ? aStream>>ceShen;//讀取測深, qint16

? ? ? ? index=theModel->index(i,0);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(ceShen,Qt::DisplayRole);


? ? ? ? aStream>>chuiShen;//垂深,qreal

? ? ? ? index=theModel->index(i,1);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(chuiShen,Qt::DisplayRole);



? ? ? ? aStream>>fangWei;//方位,qreal

? ? ? ? index=theModel->index(i,2);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(fangWei,Qt::DisplayRole);



? ? ? ? aStream>>weiYi;//位移,qreal

? ? ? ? index=theModel->index(i,3);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(weiYi,Qt::DisplayRole);



? ? ? ? aStream>>zhiLiang;//固井質量,QString

? ? ? ? index=theModel->index(i,4);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(zhiLiang,Qt::DisplayRole);


? ? ? ? aStream>>quYang;//bool

? ? ? ? index=theModel->index(i,5);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? if (quYang)

? ? ? ? ? ? aItem->setCheckState(Qt::Checked);

? ? ? ? else

? ? ? ? ? ? aItem->setCheckState(Qt::Unchecked);

? ? }


? ? aFile.close();

? ? return true;

}




第 三 個的;



//3.0

void MainWindow::on_actSave_triggered()

{ //以Qt預定義編碼保存數據文件

? ? QString curPath=QDir::currentPath();

? ? QString aFileName=QFileDialog::getSaveFileName(this,tr("選擇保存文件"),curPath,

? ? ? ? ? ? ? ? "Qt預定義編碼數據文件(*.stm)");

? ? if (aFileName.isEmpty())

? ? ? ? return; //

? if? (saveDataAsStream(aFileName)) //保存為流數據文件

? ? ? QMessageBox::information(this,"提示消息","文件已經成功保存!");

}




全部的代碼:


bool MainWindow::saveDataAsStream(QString &aFileName)

{//將模型數據保存為Qt預定義編碼的數據文件

? ? QFile aFile(aFileName);? //以文件方式讀出

? ? if (!(aFile.open(QIODevice::WriteOnly | QIODevice::Truncate)))

? ? ? ? return false;


? ? QDataStream aStream(&aFile);

? ? aStream.setVersion(QDataStream::Qt_5_9); //設置版本號,寫入和讀取的版本號要兼容


? ? qint16? rowCount=theModel->rowCount(); //數據模型行數

? ? qint16? colCount=theModel->columnCount(); //數據模型列數


? ? aStream<<rowCount; //寫入文件流,行數

? ? aStream<<colCount;//寫入文件流,列數


//獲取表頭文字

? ? for (int i=0;i<theModel->columnCount();i++)

? ? {

? ? ? ? QString str=theModel->horizontalHeaderItem(i)->text();//獲取表頭文字

? ? ? ? aStream<<str; //字符串寫入文件流,Qt預定義編碼方式

? ? }



//獲取數據區(qū)的數據

? ? for (int i=0;i<theModel->rowCount();i++)

? ? {

? ? ? ? QStandardItem* aItem=theModel->item(i,0); //測深

? ? ? ? qint16 ceShen=aItem->data(Qt::DisplayRole).toInt();

? ? ? ? aStream<<ceShen;// 寫入文件流,qint16


? ? ? ? aItem=theModel->item(i,1); //垂深

? ? ? ? qreal chuiShen=aItem->data(Qt::DisplayRole).toFloat();

? ? ? ? aStream<<chuiShen;//寫入文件流, qreal


? ? ? ? aItem=theModel->item(i,2); //方位

? ? ? ? qreal fangWei=aItem->data(Qt::DisplayRole).toFloat();

? ? ? ? aStream<<fangWei;//寫入文件流, qreal


? ? ? ? aItem=theModel->item(i,3); //位移

? ? ? ? qreal weiYi=aItem->data(Qt::DisplayRole).toFloat();

? ? ? ? aStream<<weiYi;//寫入文件流, qreal


? ? ? ? aItem=theModel->item(i,4); //固井質量

? ? ? ? QString zhiLiang=aItem->data(Qt::DisplayRole).toString();

? ? ? ? aStream<<zhiLiang;// 寫入文件流,字符串


? ? ? ? aItem=theModel->item(i,5); //測井

? ? ? ? bool quYang=(aItem->checkState()==Qt::Checked);

? ? ? ? aStream<<quYang;// 寫入文件流,bool型

? ? }

? ? aFile.close();

? ? return true;

}




第 四 個的;void on_actOpenBin_triggered(); //4.打開自編碼二進制文件


//4.

void MainWindow::on_actOpenBin_triggered()

{//打開二進制文件

? ? QString curPath=QDir::currentPath();//系統(tǒng)當前目錄

? ? QString aFileName=QFileDialog::getOpenFileName(this,tr("打開一個文件"),curPath,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "二進制數據文件(*.dat)");

? ? if (aFileName.isEmpty())

? ? ? ? return; //

? ? if? (openBinaryFile(aFileName)) //保存為流數據文件

? ? ? ? QMessageBox::information(this,"提示消息","文件已經打開!");

}




封裝的函數 :


//4.

bool MainWindow::openBinaryFile(QString &aFileName)

{//打開二進制文件

? ? QFile aFile(aFileName);? //以文件方式讀出

? ? if (!(aFile.open(QIODevice::ReadOnly)))

? ? ? ? return false;

? ? QDataStream aStream(&aFile); //用文本流讀取文件

//? ? aStream.setVersion(QDataStream::Qt_5_9); //設置數據流的版本

? ? aStream.setByteOrder(QDataStream::LittleEndian);

//? ? aStream.setByteOrder(QDataStream::BigEndian);

? ? qint16? rowCount,colCount;

? ? aStream.readRawData((char *)&rowCount, sizeof(qint16));

? ? aStream.readRawData((char *)&colCount, sizeof(qint16));

? ? this->resetTable(rowCount);

? ? //獲取表頭文字,但是并不利用

? ? char *buf;

? ? uint strLen;? //也就是 quint32

? ? for (int i=0;i

? ? {

? ? ? ? aStream.readBytes(buf,strLen);//同時讀取字符串長度,和字符串內容

? ? ? ? QString str=QString::fromLocal8Bit(buf,strLen); //可處理漢字

? ? }

//獲取數據區(qū)數據

? ? QStandardItem? *aItem;

? ? qint16? ceShen;

? ? qreal? chuiShen;

? ? qreal? fangWei;

? ? qreal? weiYi;

? ? QString? zhiLiang;

? ? qint8? quYang; //分別代表邏輯值 true和false

? ? QModelIndex index;

? ? for (int i=0;i

? ? {

? ? ? ? aStream.readRawData((char *)&ceShen, sizeof(qint16)); //測深

? ? ? ? index=theModel->index(i,0);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(ceShen,Qt::DisplayRole);

? ? ? ? aStream.readRawData((char *)&chuiShen, sizeof(qreal)); //垂深

? ? ? ? index=theModel->index(i,1);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(chuiShen,Qt::DisplayRole);

? ? ? ? aStream.readRawData((char *)&fangWei, sizeof(qreal)); //方位

? ? ? ? index=theModel->index(i,2);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(fangWei,Qt::DisplayRole);

? ? ? ? aStream.readRawData((char *)&weiYi, sizeof(qreal)); //位移

? ? ? ? index=theModel->index(i,3);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(weiYi,Qt::DisplayRole);

? ? ? ? aStream.readBytes(buf,strLen);//固井質量

? ? ? ? zhiLiang=QString::fromLocal8Bit(buf,strLen);

? ? ? ? index=theModel->index(i,4);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? aItem->setData(zhiLiang,Qt::DisplayRole);

? ? ? ? aStream.readRawData((char *)&quYang, sizeof(qint8)); //測井取樣

? ? ? ? index=theModel->index(i,5);

? ? ? ? aItem=theModel->itemFromIndex(index);

? ? ? ? if (quYang==1)

? ? ? ? ? ? aItem->setCheckState(Qt::Checked);

? ? ? ? else

? ? ? ? ? ? aItem->setCheckState(Qt::Unchecked);

? ? }

? ? aFile.close();

? ? return true;

}





void on_actSaveBin_triggered(); //5.保存為自編碼二進制文件


void MainWindow::on_actSaveBin_triggered()

{//保存二進制文件

? ? QString curPath=QDir::currentPath();

? ? //調用打開文件對話框選擇一個文件

? ? QString aFileName=QFileDialog::getSaveFileName(this,tr("選擇保存文件"),curPath,

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "二進制數據文件(*.dat)");

? ? if (aFileName.isEmpty())

? ? ? ? return; //

? ? if? (saveBinaryFile(aFileName)) //保存為流數據文件

? ? ? ? QMessageBox::information(this,"提示消息","文件已經成功保存!");

}









下面的 看 主函數 :



MainWindow::MainWindow(QWidget *parent) :

? ? QMainWindow(parent),

? ? ui(new Ui::MainWindow)

{

? ? ui->setupUi(this);


? ? theModel = new QStandardItemModel(5,FixedColumnCount,this); //創(chuàng)建數據模型

? ? QStringList? ? ?headerList;

? ? headerList<<"Depth"<<"Measured Depth"<<"Direction"<<"Offset"<<"Quality"<<"Sampled";

? ? theModel->setHorizontalHeaderLabels(headerList); //設置表頭文字



? ? theSelection = new QItemSelectionModel(theModel);//Item選擇模型

? ? connect(theSelection,SIGNAL(currentChanged(QModelIndex,QModelIndex)),

? ? ? ? ? ? this,SLOT(on_currentChanged(QModelIndex,QModelIndex)));


? ? //為tableView設置數據模型

? ? ui->tableView->setModel(theModel); //設置數據模型

? ? ui->tableView->setSelectionModel(theSelection);//設置選擇模型


//為各列設置自定義代理組件

? ? ui->tableView->setItemDelegateForColumn(0,&intSpinDelegate);? //測深,整數

? ? ui->tableView->setItemDelegateForColumn(1,&floatSpinDelegate);? //浮點數

? ? ui->tableView->setItemDelegateForColumn(2,&floatSpinDelegate); //浮點數

? ? ui->tableView->setItemDelegateForColumn(3,&floatSpinDelegate); //浮點數

? ? ui->tableView->setItemDelegateForColumn(4,&comboBoxDelegate); //Combbox選擇型



? ? resetTable(5); //表格復位


? ? setCentralWidget(ui->tabWidget); //


//創(chuàng)建狀態(tài)欄組件

? ? LabCellPos = new QLabel("當前單元格:",this);

? ? LabCellPos->setMinimumWidth(180);

? ? LabCellPos->setAlignment(Qt::AlignHCenter);


? ? LabCellText = new QLabel("單元格內容:",this);

? ? LabCellText->setMinimumWidth(200);


? ? ui->statusBar->addWidget(LabCellPos);

? ? ui->statusBar->addWidget(LabCellText);

}






這么晚

怎么玩的 :

分析:

這段代碼是一個 Qt 應用程序中 `MainWindow` 類的構造函數實現。`MainWindow` 繼承自 `QMainWindow`,用于設置主窗口的初始狀態(tài)和初始化組件。下面是對這段代碼的詳細分析:

### 1. **構造函數初始化列表**

```cpp

MainWindow::MainWindow(QWidget *parent) :

? ? QMainWindow(parent),

? ? ui(new Ui::MainWindow)

{

```

- `QMainWindow(parent)`:調用基類 `QMainWindow` 的構造函數,并將父窗口指針 `parent` 傳遞給它。這確保了 `MainWindow` 可以作為其他窗口的子窗口。

- `ui(new Ui::MainWindow)`:初始化 `ui` 指針,`Ui::MainWindow` 是由 Qt 的用戶界面設計器(Qt Designer)生成的類,用于設置和管理窗口的控件。

### 2. **設置 UI**

```cpp

? ? ui->setupUi(this);

```

- `setupUi(this)`:調用 `Ui::MainWindow` 的 `setupUi` 方法,用于初始化界面組件并將它們添加到 `MainWindow` 中。

### 3. **創(chuàng)建和設置數據模型**

```cpp

? ? theModel = new QStandardItemModel(5, FixedColumnCount, this); // 創(chuàng)建數據模型

? ? QStringList headerList;

? ? headerList << "Depth" << "Measured Depth" << "Direction" << "Offset" << "Quality" << "Sampled";

? ? theModel->setHorizontalHeaderLabels(headerList); // 設置表頭文字

```

- `QStandardItemModel(5, FixedColumnCount, this)`:創(chuàng)建一個 `QStandardItemModel` 實例,具有 5 行和 `FixedColumnCount` 列(`FixedColumnCount` 應該是一個預定義的常量或變量,指定列數)。

- `headerList`:一個 `QStringList`,用來定義表格的列標題。

- `setHorizontalHeaderLabels(headerList)`:設置表格的列標題。

### 4. **設置選擇模型**

```cpp

? ? theSelection = new QItemSelectionModel(theModel); // Item選擇模型

? ? connect(theSelection, SIGNAL(currentChanged(QModelIndex, QModelIndex)),

? ? ? ? ? ? this, SLOT(on_currentChanged(QModelIndex, QModelIndex)));

```

- `QItemSelectionModel(theModel)`:創(chuàng)建一個 `QItemSelectionModel` 實例,用于管理選中的表格項。

- `connect()`:連接信號和槽。當選擇變化時(`currentChanged` 信號),調用 `on_currentChanged` 槽函數。

### 5. **設置表格視圖**

```cpp

? ? ui->tableView->setModel(theModel); // 設置數據模型

? ? ui->tableView->setSelectionModel(theSelection); // 設置選擇模型

```

- `setModel(theModel)`:將創(chuàng)建的數據模型 `theModel` 設置到 `tableView` 中。

- `setSelectionModel(theSelection)`:將選擇模型 `theSelection` 設置到 `tableView` 中。

### 6. **設置自定義代理**

```cpp

? ? ui->tableView->setItemDelegateForColumn(0, &intSpinDelegate);? // 測深,整數

? ? ui->tableView->setItemDelegateForColumn(1, &floatSpinDelegate); // 浮點數

? ? ui->tableView->setItemDelegateForColumn(2, &floatSpinDelegate); // 浮點數

? ? ui->tableView->setItemDelegateForColumn(3, &floatSpinDelegate); // 浮點數

? ? ui->tableView->setItemDelegateForColumn(4, &comboBoxDelegate); // ComboBox選擇型

```

- `setItemDelegateForColumn(column, delegate)`:為指定列設置自定義代理組件。代理負責處理該列的編輯和顯示。例如,整數、浮點數和下拉框選擇器。

### 7. **復位表格**

```cpp

? ? resetTable(5); // 表格復位

```

- `resetTable(5)`:調用 `resetTable` 方法,可能用于初始化或清空表格的數據。具體實現取決于 `resetTable` 函數的定義。

### 8. **設置中央部件**

```cpp

? ? setCentralWidget(ui->tabWidget); //

```

- `setCentralWidget(ui->tabWidget)`:將 `tabWidget` 設置為 `MainWindow` 的中央部件。`tabWidget` 是 `ui` 中的一個組件,通常用于顯示選項卡。

### 9. **創(chuàng)建和設置狀態(tài)欄組件**

```cpp

? ? LabCellPos = new QLabel("當前單元格:", this);

? ? LabCellPos->setMinimumWidth(180);

? ? LabCellPos->setAlignment(Qt::AlignHCenter);

? ? LabCellText = new QLabel("單元格內容:", this);

? ? LabCellText->setMinimumWidth(200);

? ? ui->statusBar->addWidget(LabCellPos);

? ? ui->statusBar->addWidget(LabCellText);

```

- `QLabel`:創(chuàng)建兩個標簽 `LabCellPos` 和 `LabCellText`,用于在狀態(tài)欄顯示信息。

- `setMinimumWidth()` 和 `setAlignment()`:設置標簽的最小寬度和對齊方式。

- `ui->statusBar->addWidget()`:將標簽添加到窗口的狀態(tài)欄中。

### 總結

這段代碼是 `MainWindow` 類的構造函數,負責初始化用戶界面,包括設置數據模型、選擇模型、表格視圖、狀態(tài)欄以及其他 UI 組件。它通過 `Qt` 的信號和槽機制以及自定義代理來處理表格的數據展示和用戶交互。




最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容