Qt中QtTableWidget的使用

 

最近面试了一个题目(Qt相关),现在分享如下:

 

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

#ifndef DIALOG_H
#define DIALOG_H

#include <QDialog>

namespace Ui {
class Dialog;
}

class Dialog : public QDialog
{
    Q_OBJECT

public:
    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>

 

posted @ 2014-03-05 20:04  L00ng  阅读(1037)  评论(0编辑  收藏  举报