日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Qt中QtTableWidget的使用

發布時間:2024/4/17 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Qt中QtTableWidget的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

最近面試了一個題目(Qt相關),現在分享如下:

?

要求作出如圖所示的效果:

#ifndef DIALOG_H #define DIALOG_H#include <QDialog>namespace Ui { class Dialog; }class Dialog : public QDialog {Q_OBJECTpublic:explicit Dialog(QWidget *parent = 0);~Dialog();QString name;QString gender;QString age;private slots:void on_lineEditName_editingFinished();void on_lineEditGender_editingFinished();void on_pushButtonAdd_clicked();void on_pushButtonDel_clicked();void on_lineEditAge_editingFinished();private:Ui::Dialog *ui;};#endif // DIALOG_H

?

#include "dialog.h" #include "ui_dialog.h"Dialog::Dialog(QWidget *parent) :QDialog(parent),ui(new Ui::Dialog) {ui->setupUi(this); }Dialog::~Dialog() {delete ui; }void Dialog::on_lineEditName_editingFinished() {name = ui->lineEditName->displayText();}void Dialog::on_lineEditGender_editingFinished() {gender = ui->lineEditGender->displayText(); } void Dialog::on_lineEditAge_editingFinished() {age = ui->lineEditAge->displayText(); } void Dialog::on_pushButtonAdd_clicked() {ui->tableWidget->setColumnCount(3);ui->tableWidget->insertRow(ui->tableWidget->rowCount());ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行選中的方式 QTableWidgetItem * item1 = new QTableWidgetItem(name);QTableWidgetItem * item2 = new QTableWidgetItem(gender);QTableWidgetItem * item3 = new QTableWidgetItem(age);ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,0,item1);ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,1,item2);ui->tableWidget->setItem(ui->tableWidget->rowCount()-1,2,item3); }void Dialog::on_pushButtonDel_clicked() {ui->tableWidget->removeRow(ui->tableWidget->currentItem()->row()); } #include "dialog.h" #include <QApplication>int main(int argc, char *argv[]) {QApplication a(argc, argv);Dialog w;w.show();return a.exec(); } <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"><class>Dialog</class><widget class="QDialog" name="Dialog"><property name="geometry"><rect><x>0</x><y>0</y><width>516</width><height>324</height></rect></property><property name="windowTitle"><string>Dialog</string></property><widget class="QTableWidget" name="tableWidget"><property name="geometry"><rect><x>0</x><y>0</y><width>291</width><height>231</height></rect></property></widget><widget class="QLabel" name="labelName"><property name="geometry"><rect><x>320</x><y>50</y><width>54</width><height>12</height></rect></property><property name="text"><string>姓名</string></property></widget><widget class="QLabel" name="labelGender"><property name="geometry"><rect><x>320</x><y>140</y><width>54</width><height>12</height></rect></property><property name="text"><string>性別</string></property></widget><widget class="QLineEdit" name="lineEditName"><property name="geometry"><rect><x>390</x><y>50</y><width>113</width><height>20</height></rect></property></widget><widget class="QPushButton" name="pushButtonAdd"><property name="geometry"><rect><x>70</x><y>270</y><width>75</width><height>23</height></rect></property><property name="text"><string>添加</string></property></widget><widget class="QPushButton" name="pushButtonDel"><property name="geometry"><rect><x>230</x><y>270</y><width>75</width><height>23</height></rect></property><property name="text"><string>刪除</string></property></widget><widget class="QPushButton" name="pushButtonEdit"><property name="geometry"><rect><x>380</x><y>270</y><width>75</width><height>23</height></rect></property><property name="text"><string>修改</string></property></widget><widget class="QLineEdit" name="lineEditGender"><property name="geometry"><rect><x>390</x><y>130</y><width>113</width><height>20</height></rect></property></widget><widget class="QLineEdit" name="lineEditAge"><property name="geometry"><rect><x>390</x><y>200</y><width>113</width><height>20</height></rect></property></widget><widget class="QLabel" name="labelAge"><property name="geometry"><rect><x>320</x><y>210</y><width>54</width><height>12</height></rect></property><property name="text"><string>出生年月</string></property></widget></widget><layoutdefault spacing="6" margin="11"/><tabstops><tabstop>lineEditName</tabstop><tabstop>lineEditGender</tabstop><tabstop>lineEditAge</tabstop><tabstop>pushButtonAdd</tabstop><tabstop>pushButtonDel</tabstop><tabstop>pushButtonEdit</tabstop><tabstop>tableWidget</tabstop></tabstops><resources/><connections/> </ui>

?

轉載于:https://www.cnblogs.com/loongcheng/p/3583247.html

總結

以上是生活随笔為你收集整理的Qt中QtTableWidget的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。