Qt中QtTableWidget的使用
生活随笔
收集整理的這篇文章主要介紹了
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的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 初学SSH 配置+错误总结
- 下一篇: Jbox帮助文档,默认的属性含义