QT: 操作主从视图及XML

按钮绑定信号、槽

控件属性

 

 运行效果:

 

 

connectdlg.h 

  

connectdlg.cpp 

  

connectdlg.ui 详细代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>ConnDlg</class>
 <widget class="QDialog" name="ConnDlg">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>450</width>
    <height>397</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>数据库连接</string>
  </property>
  <widget class="QGroupBox" name="connGroupBox">
   <property name="geometry">
    <rect>
     <x>10</x>
     <y>10</y>
     <width>431</width>
     <height>261</height>
    </rect>
   </property>
   <property name="title">
    <string>数据库连接设置</string>
   </property>
   <widget class="QComboBox" name="comboDriver">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>30</y>
      <width>221</width>
      <height>22</height>
     </rect>
    </property>
   </widget>
   <widget class="QLineEdit" name="editDatabase">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>70</y>
      <width>181</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>databasefile.db</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="editUsername">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>100</y>
      <width>181</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>admin</string>
    </property>
   </widget>
   <widget class="QLineEdit" name="editPassword">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>130</y>
      <width>181</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>123456</string>
    </property>
    <property name="echoMode">
     <enum>QLineEdit::Password</enum>
    </property>
    <property name="placeholderText">
     <string/>
    </property>
   </widget>
   <widget class="QLineEdit" name="editHostname">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>160</y>
      <width>181</width>
      <height>21</height>
     </rect>
    </property>
    <property name="text">
     <string>localhost</string>
    </property>
   </widget>
   <widget class="QSpinBox" name="portSpinBox">
    <property name="geometry">
     <rect>
      <x>170</x>
      <y>190</y>
      <width>181</width>
      <height>22</height>
     </rect>
    </property>
    <property name="maximum">
     <number>65535</number>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel2">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>30</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>驱动:</string>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel3">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>70</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>数据库名:</string>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel4">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>100</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>用户名:</string>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel4_2">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>130</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>密码:</string>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel5">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>170</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>主机名:</string>
    </property>
   </widget>
   <widget class="QLabel" name="textLabel5_2">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>200</y>
      <width>72</width>
      <height>15</height>
     </rect>
    </property>
    <property name="text">
     <string>端口:</string>
    </property>
   </widget>
  </widget>
  <widget class="QLabel" name="status_label">
   <property name="geometry">
    <rect>
     <x>30</x>
     <y>290</y>
     <width>391</width>
     <height>16</height>
    </rect>
   </property>
   <property name="text">
    <string>状态:</string>
   </property>
  </widget>
  <widget class="QWidget" name="horizontalLayoutWidget">
   <property name="geometry">
    <rect>
     <x>20</x>
     <y>330</y>
     <width>401</width>
     <height>41</height>
    </rect>
   </property>
   <layout class="QHBoxLayout" name="horizontalLayout">
    <item>
     <spacer name="horizontalSpacer">
      <property name="orientation">
       <enum>Qt::Horizontal</enum>
      </property>
      <property name="sizeHint" stdset="0">
       <size>
        <width>40</width>
        <height>20</height>
       </size>
      </property>
     </spacer>
    </item>
    <item>
     <widget class="QPushButton" name="okButton">
      <property name="text">
       <string>连接</string>
      </property>
     </widget>
    </item>
    <item>
     <widget class="QPushButton" name="cancelButton">
      <property name="text">
       <string>取消</string>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
 </widget>
 <resources/>
 <connections>
  <connection>
   <sender>cancelButton</sender>
   <signal>clicked()</signal>
   <receiver>ConnDlg</receiver>
   <slot>reject()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>373</x>
     <y>349</y>
    </hint>
    <hint type="destinationlabel">
     <x>224</x>
     <y>198</y>
    </hint>
   </hints>
  </connection>
  <connection>
   <sender>okButton</sender>
   <signal>clicked()</signal>
   <receiver>ConnDlg</receiver>
   <slot>accept()</slot>
   <hints>
    <hint type="sourcelabel">
     <x>274</x>
     <y>349</y>
    </hint>
    <hint type="destinationlabel">
     <x>224</x>
     <y>198</y>
    </hint>
   </hints>
  </connection>
 </connections>
</ui>

mainwindow.ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1057</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <widget class="QGroupBox" name="createFactoryGroupBox">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>20</y>
      <width>561</width>
      <height>201</height>
     </rect>
    </property>
    <property name="title">
     <string>汽车制造商</string>
    </property>
   </widget>
   <widget class="QGroupBox" name="createCarGroupBox">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>230</y>
      <width>561</width>
      <height>231</height>
     </rect>
    </property>
    <property name="title">
     <string>汽车</string>
    </property>
   </widget>
   <widget class="QGroupBox" name="createDetailsGroupBox">
    <property name="geometry">
     <rect>
      <x>600</x>
      <y>20</y>
      <width>441</width>
      <height>441</height>
     </rect>
    </property>
    <property name="title">
     <string>详细信息</string>
    </property>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menubar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>1057</width>
     <height>26</height>
    </rect>
   </property>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>

  

  

mainwindow.cpp 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#include "mainwindow.h"
#include "ui_mainwindow.h"
 
#include <QGridLayout>
#include <QAbstractItemView>
#include <QHeaderView>
#include <QAction>
#include <QMenu>
#include <QMenuBar>
#include <QDialog>
 
MainWindow::MainWindow(const QString &factoryTable, const QString &carTable,
                       QFile *carDetails, QWidget *parent)
    : QMainWindow(parent)
   // , ui(new Ui::MainWindow)
{
   // ui->setupUi(this);
    file = carDetails;
        readCarData();
        carModel = new QSqlRelationalTableModel(this);
        carModel->setTable(carTable);
        carModel->setRelation(2,QSqlRelation(factoryTable,"id","manufactory"));
        carModel->select();
 
        factoryModel = new QSqlTableModel(this);
        factoryModel->setTable(factoryTable);
        factoryModel->select();
 
        QGroupBox *factory = createFactoryGroupBox();
        QGroupBox *cars = createCarGroupBox();
        QGroupBox *details = createDetailsGroupBox();
        // 布局
        QGridLayout *layout = new QGridLayout;
        layout->addWidget(factory,0,0);
        layout->addWidget(cars,1,0);
        layout->addWidget(details,0,1,2,1);
        layout->setColumnStretch(1,1);
        layout->setColumnMinimumWidth(0,500);
        QWidget *widget = new QWidget;
        widget->setLayout(layout);
        setCentralWidget(widget);
        createMenuBar();
        resize(850,400);
        setWindowTitle(tr("主从视图"));
}
 
MainWindow::~MainWindow()
{
   // delete ui;
}
 
 
 
void MainWindow::addCar()
{
    //
    /*
    Dialog *dialog = new QDialog(carModel, factoryModel,carData, file, this);
    int accepted = dialog->exec();
    if(accepted ==1 ){
        int lastRow = carModel->rowCount()-1;
        carView->selectRow(lastRow);
        carView->scrollToBottom();
        showCarDetails(carModel->index(lastRow,0));
    }
    */
}
 
void MainWindow::changeFactory(QModelIndex index)
{
    QSqlRecord record = factoryModel->record(index.row());
    QString factoryId = record.value("id").toString();
    carModel->setFilter("id = '"+ factoryId + "'");
    showFactoryProfile(index);
}
 
void MainWindow::delCar()
{
    QModelIndexList selection = carView->selectionModel()->selectedRows(0);
    if(!selection.empty()){
        QModelIndex idIndex = selection.at(0);
      int id = idIndex.data().toInt();
      QString name = idIndex.sibling(idIndex.row(),1).data().toString();
      QString factory = idIndex.sibling(idIndex.row(),2).data().toString();
      QMessageBox::StandardButton button;
      button =QMessageBox::question(this, tr("删除汽车记录"),QString(tr("确认删除由'%1'生产的'%2'吗?").arg(factory).arg(name)),QMessageBox::Yes|QMessageBox::No);
      if(button == QMessageBox::Yes){
          removeCarFromFile(id);
          removeCarFromDatabase(idIndex);
          decreaseCarCount(indexOfFactory(factory));
      }
      else{
          QMessageBox::information(this,tr("删除汽车记录"),tr("请选择要删除的记录"));
      }
    }
}
 
void MainWindow::showCarDetails(QModelIndex index)
{
    QSqlRecord record = carModel->record(index.row());
    QString factory = record.value("manufactory").toString();
    QString name = record.value("name").toString();
    QString year = record.value("year").toString();
    QString carId = record.value("carid").toString();
    showFactoryProfile(indexOfFactory(factory));
    titleLabel->setText(tr("品牌:%1 (%2)").arg(name).arg(year));
    titleLabel->show();
    QDomNodeList cars = carData.elementsByTagName("car");
    for (int i = 0; i<cars.count(); i++)
    {
        QDomNode car = cars.item(i);
        if(car.toElement().attribute("id")==carId){
            getAttribList(car.toElement());
            break;
        }
    }
    if( (!attribList->count()) == 0){
        attribList->show();
    }
}
 
void MainWindow::showFactoryProfile(QModelIndex index)
{
    QSqlRecord record = factoryModel->record(index.row());
    QString name = record.value("manufactory").toString();
    int count = carModel->rowCount();
    profileLabel->setText(tr("汽车制造商:%1\n产品数量:%2").arg(name).arg(count));
    profileLabel->show();
    titleLabel->hide();
    attribList->hide();
}
 
QGroupBox *MainWindow::createFactoryGroupBox()
{
    factoryView = new QTableView;
    factoryView->setEditTriggers(QAbstractItemView::NoEditTriggers);
 
    factoryView->setSortingEnabled(true);
    factoryView->setSelectionBehavior(QAbstractItemView::SelectRows);
    factoryView->setSelectionMode(QAbstractItemView::SingleSelection);
    factoryView->setShowGrid(false);
    factoryView->setAlternatingRowColors(true);
    factoryView->setModel(factoryModel);
 
    connect(factoryView, SIGNAL(clicked(QModelIndex)), this, SLOT(changeFactory(QModelIndex)));
    QGroupBox *box = new QGroupBox(tr("汽车制造商"));
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(factoryView,0,0);
    box->setLayout(layout);
    return box;
}
 
QGroupBox *MainWindow::createCarGroupBox()
{
    //
    QGroupBox *box = new QGroupBox(tr("汽车"));
    carView = new QTableView;
    carView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    carView->setSortingEnabled(true);
    carView->setSelectionBehavior(QAbstractItemView::SelectRows);
    carView->setSelectionMode(QAbstractItemView::SingleSelection);
    carView->setShowGrid(false);
    carView->verticalHeader()->hide();
    carView->setAlternatingRowColors(true);
 
    carView->setModel(carModel);
    connect(carView, SIGNAL(clicked(QModelIndex)), this, SLOT(showCarDetails(QModelIndex)));
    connect(carView, SIGNAL(activated(QModelIndex)), this, SLOT(showCarDetails(QModelIndex)));
 
    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(carView,0,0);
    box->setLayout(layout);
    return box;
}
 
QGroupBox *MainWindow::createDetailsGroupBox()
{
    //
    QGroupBox *box = new QGroupBox(tr("详细信息"));
    profileLabel = new QLabel;
    profileLabel->setWordWrap(true);
    profileLabel->setAlignment(Qt::AlignBottom);
    titleLabel = new QLabel;
    titleLabel->setWordWrap(true);
    titleLabel->setAlignment(Qt::AlignBottom);
    attribList = new QListWidget;
    QGridLayout *layout = new QGridLayout;
    layout->addWidget(profileLabel,0,0,1,2);
    layout->addWidget(titleLabel,1,0,1,2);
    layout->addWidget(attribList,2,0,1,2);
    layout->setRowStretch(2,1);
    box->setLayout(layout);
    return box;
}
 
void MainWindow::createMenuBar()
{
    // 菜单函数
    QAction *addAction = new QAction(tr("添加"),this);
    QAction *deleteAction = new QAction(tr("删除"),this);
    QAction *quitAction = new QAction(tr("退出"),this);
    addAction->setShortcut(tr("Ctrl+A"));
    deleteAction->setShortcut(tr("Ctrl+D"));
    quitAction->setShortcut(tr("Ctrl+Q"));
    QMenu *fileMenu = menuBar()->addMenu(tr("操作菜单"));
    fileMenu->addAction(addAction);
    fileMenu->addAction(deleteAction);
    fileMenu->addSeparator();
    fileMenu->addAction(quitAction);
 
    connect(addAction, SIGNAL(triggered(bool)), this, SLOT(addCar()));
    connect(deleteAction, SIGNAL(triggered(bool)), this, SLOT(delCar()));
    connect(quitAction, SIGNAL(triggered(bool)), this, SLOT(close()));
}
 
void MainWindow::decreaseCarCount(QModelIndex index)
{
    int row = index.row();
    int count = carModel->rowCount();
    if(count == 0)
    {
        factoryModel->removeRow(row);
    }
}
 
void MainWindow::getAttribList(QDomNode car)
{
    attribList->clear();
    QDomNodeList attribs = car.childNodes();
    QDomNode node;
    QString attribNumber;
    for(int j = 0; j<attribs.count(); j++){
        node = attribs.item(j);
        attribNumber = node.toElement().attribute("number");
        QListWidgetItem *item = new QListWidgetItem(attribList);
        QString showText(attribNumber + ": " + node.toElement().text());
        item->setText(tr("%1").arg(showText));
 
    }
}
 
QModelIndex MainWindow::indexOfFactory(const QString &factory)
{
    for(int i =0 ;i < factoryModel->rowCount(); i++){
        QSqlRecord record = factoryModel->record(i);
        if(record.value("manufactory")== factory)
            return factoryModel->index(i,1);
    }
    return QModelIndex();
}
 
void MainWindow::readCarData()
{
    if(!file->open(QIODevice::ReadOnly))
        return;
    if(!carData.setContent(file))
    {
        file->close();
        return;
    }
    file->close();
}
 
void MainWindow::removeCarFromDatabase(QModelIndex index)
{
    carModel->removeRow(index.row());
}
 
void MainWindow::removeCarFromFile(int id)
{
    QDomNodeList cars = carData.elementsByTagName("car");
    for(int i = 0; i<cars.count(); i++){
        QDomNode node = cars.item(i);
        if(node.toElement().attribute("id").toInt()==id){
            carData.elementsByTagName("archive").item(0).removeChild(node);
            break;
        }
    }
}

  

mainwindow.h 

  

 

attribs.xml 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?xml version="1.0" encoding="utf-8"?>
<archive>
  <car id="1">
    <attrib number="01">排量:2393ml</attrib>
        <attrib number="02">价格:43.26万元</attrib>
        <attrib number="03">排放:欧4</attrib>
        <attrib number="04">油耗:7.01(90km/h)</attrib>
        <attrib number="05">功率:130/6000</attrib>
  </car>
  <car id="2">
    <attrib number="01">排量:1600ml</attrib>
        <attrib number="02">价格:8.98万元</attrib>
        <attrib number="03">排放:欧3</attrib>
        <attrib number="04">油耗:6.11(90km/h)</attrib>
        <attrib number="05">功率:68/5800</attrib>
  </car>
  <car id="3">
    <attrib number="01">排量:1600ml</attrib>
        <attrib number="02">价格:11.25万元</attrib>
        <attrib number="03">排放:欧3带OBD</attrib>
        <attrib number="04">油耗:6.01(90km/h)</attrib>
        <attrib number="05">功率:74/6000</attrib>
  </car>
  <car id="4">
    <attrib number="01">排量:1997ml</attrib>
        <attrib number="02">价格:15.38万元</attrib>
        <attrib number="03">排放:欧3带OBD</attrib>
        <attrib number="04">油耗:7.01(90km/h)</attrib>
        <attrib number="05">功率:130/6000</attrib>
  </car>
  <car id="5">
    <attrib number="01">排量:1600ml</attrib>
        <attrib number="02">价格:6.58万元</attrib>
        <attrib number="03">排放:欧3</attrib>
        <attrib number="04">油耗:6.51(90km/h)</attrib>
        <attrib number="05">功率:65/5600</attrib>
  </car>
  <car id="6">
    <attrib number="01">排量:1997ml</attrib>
        <attrib number="02">价格:16.08万元</attrib>
        <attrib number="03">排放:欧4</attrib>
        <attrib number="04">油耗:7.01(90km/h)</attrib>
        <attrib number="05">功率:108/6000</attrib>
  </car>
  <car id="7">
    <attrib number="01">排量:1781ml</attrib>
        <attrib number="02">价格:7.98万元</attrib>
        <attrib number="03">排放:欧3</attrib>
        <attrib number="04">油耗:7.21(90km/h)</attrib>
        <attrib number="05">功率:70/5200</attrib>
  </car>
  <car id="8">
    <attrib number="01">排量:1984ml</attrib>
        <attrib number="02">价格:19.58万元</attrib>
        <attrib number="03">排放:欧4</attrib>
        <attrib number="04">油耗:7.11(90km/h)</attrib>
        <attrib number="05">功率:85/5400</attrib>
  </car>
</archive>

  

posted @   samrv  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2022-11-13 EBS:限定词栏的字符串中间增加了“_0x000D_”的字符问题处理
点击右上角即可分享
微信分享提示