QT: sort dialog

本文详细介绍了使用QtDesigner创建对话框界面的过程,包括按钮、布局、分组框等元素的添加与配置,并通过代码实现对话框的功能定制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

new a dialog, add some buttons, button is an object which has a name and has a text.

select some buttons, then form/lay out ver or h

new a groupbox, add some objects, then form/lay out grid

Edit/edit widget. signal slot. tab order

 

 

1. Click File|New Form and choose the "Dialog without Buttons" template.

 

2. Create an OK button and drag it to the top right of the form. Change its objectName to "okButton" and set its default property to "true".  

3Create a Cancel button, and drag it below the OK button. Change its objectName to "cancelButton".

4. Create a vertical spacer and drag it below the Cancel button, then create a More button and drag it below the vertical spacer. Change the More button's objectName to "moreButton", set its text property to "&More", and its checkable property to "true".

5. Click the OK button, then Shift+Click the Cancel button, the vertical spacer, and the More button, then click Form|Lay Out Vertically.生成右边的lay out

 

 

6. Create a group box, two labels, two comboboxes, and one horizontal spacer, and put them
anywhere on the form.

7. Right-click the first combobox and choose Edit Items from the context menu to pop up Qt
Designer's combobox editor. Create one item with the text "None".
8. Right-click the second combobox and choose Edit Items. Create an "Ascending" item and a
"Descending" item.

9.Hold down the Ctrl key (Alt on the Mac) and click and drag the Primary Key group box to create a
copy of the group box (and its contents) on top of the original.

10.Click Edit|Edit Signals/Slots to enter Qt Designer's connection mode,To establish a connection between two widgets, click the sender widget  "OK Button" and drag the red arrow line to the receiver widget"group box 2", then release. select toggeled signal and slot setvisible

11.connect the okButton and the form's accept() slot.Drag the red arrow line from the okButton to an empty part of the form, then release.

 

Save the dialog as sortdialog.ui

 

create a sortdialog.h

#ifndef SORTDIALOG_H
#define SORTDIALOG_H
#include <QDialog>
#include "ui_sortdialog.h"
class SortDialog : public QDialog, public Ui::SortDialog
{
Q_OBJECT
public:
SortDialog(QWidget *parent = 0);
void setColumnRange(QChar first, QChar last);
};
#endif

 

create sortdialog.cpp:

 

1 #include <QtGui>
2 #include "sortdialog.h"

3 SortDialog::SortDialog(QWidget *parent)
4 : QDialog(parent)
5 {
6 setupUi(this);


7 secondaryGroupBox->hide();
8 tertiaryGroupBox->hide();//hides the secondary and tertiary parts of the dialog.


9 layout()->setSizeConstraint(QLayout::SetFixedSize);//making the dialog non-resizable by the user.


10 setColumnRange('A', 'Z');
11 }
12 void SortDialog::setColumnRange(QChar first, QChar last)
13 {
14 primaryColumnCombo->clear();
15 secondaryColumnCombo->clear();
16 tertiaryColumnCombo->clear();
17 secondaryColumnCombo->addItem(tr("None"));
18 tertiaryColumnCombo->addItem(tr("None"));
19 primaryColumnCombo->setMinimumSize(
20 secondaryColumnCombo->sizeHint());//sizeHint() function returns a widget's"ideal" size


21 QChar ch = first;
22 while (ch <= last) {
23 primaryColumnCombo->addItem(QString(ch));
24 secondaryColumnCombo->addItem(QString(ch));
25 tertiaryColumnCombo->addItem(QString(ch));
26 ch = ch.unicode() + 1;
27 }
28 }

 

#include <QApplication>
#include "sortdialog.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
SortDialog *dialog = new SortDialog;
dialog->setColumnRange('C', 'F');
dialog->show();
return app.exec();
}

 

#include "c.h"//借还管理 #include "ui_c.h" #include "a.h" C::C(QWidget *parent) : QMainWindow(parent) , ui(new Ui::C) { ui->setupUi(this); this->setWindowTitle("借还管理"); QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); // SQLite示例 db.setDatabaseName("D:/qtfile/serve1/data.db"); // 数据库文件路径 if (!db.open()) { qDebug() << "数据库连接失败:" << db.lastError().text(); return; } //创建表 QSqlQuery query; if (!query.exec("CREATE TABLE IF NOT EXISTS equipment (" "id INTEGER PRIMARY KEY AUTOINCREMENT," "name TEXT NOT NULL," "type TEXT NOT NULL," "status INTEGER DEFAULT 0," "lend_out_date DATE," "return_date DATE," "people TEXT NOT NULL," "phone TEXT NOT NULL);")) { qDebug() << "建表错误:" << query.lastError().text(); } //插入两条数据 QSqlQuery checkQuery("SELECT COUNT(*) FROM equipment;"); if (checkQuery.next() && checkQuery.value(0).toInt() == 0) { QSqlQuery insertQuery; // 第一条数据 insertQuery.prepare("INSERT INTO equipment (name, type, status, lend_out_date,return_date,people,phone) " "VALUES (:name, :type, :status, :date_1, :date_2, :people, :phone)"); insertQuery.bindValue(":name", "光谱分析仪"); insertQuery.bindValue(":type", "实验室设备"); insertQuery.bindValue(":status", 0); insertQuery.bindValue(":date_1", QDate(2023, 5, 10)); insertQuery.bindValue(":date_2", QDate()); insertQuery.bindValue(":people", "张洪平"); insertQuery.bindValue(":phone", "19923688556"); if (!insertQuery.exec()) { qDebug() << "插入数据失败:" << insertQuery.lastError().text(); } // 第二条数据 insertQuery.prepare("INSERT INTO equipment (name, type, status, lend_out_date, return_date, people, phone ) " "VALUES (:name, :type, :status, :date_1, :date_2, :people, :phone)"); insertQuery.bindValue(":name", "离心机"); insertQuery.bindValue(":type", "生物设备"); insertQuery.bindValue(":status", 1); insertQuery.bindValue(":date_1", QDate(2022, 11, 20)); insertQuery.bindValue(":date_2", QDate(2023, 7, 9)); insertQuery.bindValue(":people", "张洪平"); insertQuery.bindValue(":phone", "19923688556"); if (!insertQuery.exec()) { qDebug() << "插入数据失败:" << insertQuery.lastError().text(); } } model = new QSqlTableModel(this); model->setTable("equipment"); // 设置数据库表名 model->setEditStrategy(QSqlTableModel::OnManualSubmit); // 手动提交修改 model->setSort(model->fieldIndex("ID"),Qt::AscendingOrder);//按id排序 model->select();//加载数据 // 设置列标题 model->setHeaderData(0, Qt::Horizontal, tr("ID")); model->setHeaderData(1, Qt::Horizontal, tr("设备名称")); model->setHeaderData(2, Qt::Horizontal, tr("设备类型")); model->setHeaderData(3, Qt::Horizontal, tr("借还状态")); model->setHeaderData(4, Qt::Horizontal, tr("借出日期")); model->setHeaderData(5,Qt::Horizontal, tr("归还日期")); model->setHeaderData(6, Qt::Horizontal, tr("借用人")); model->setHeaderData(7,Qt::Horizontal, tr("手机")); // 加载数据 if (!model->select()) { qDebug() << "数据加载失败:" << model->lastError().text(); } ui->tableView->setModel(model);//绑定模型 ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection); ui->tableView->setModel(model); ui->tableView->resizeColumnsToContents(); // 自动调整列宽 ui->tableView->show(); } C::~C() { delete ui; } void C::on_pushButton_clicked()//返回按钮 { A *w=new A(); w->show(); this->hide(); } void C::on_pushButton_2_clicked()//退出按钮 { this->close(); } //借出记录 void C::on_action_triggered() { model->setFilter(""); } //归还记录 void C::on_action_2_triggered(){ model->setFilter("status <> 0");// 排除状态为0的记录 } void C::on_action_3_triggered() { QDialog dialog(this); dialog.setWindowTitle("添加新记录"); dialog.setMinimumWidth(300); // 创建布局 QVBoxLayout *mainLayout = new QVBoxLayout(&dialog); QFormLayout *formLayout = new QFormLayout(); // 创建输入控件 QLineEdit *nameEdit = new QLineEdit(&dialog);//equipment QLineEdit *typeEdit = new QLineEdit(&dialog); QLineEdit *lend_out_dateEdit = new QLineEdit(&dialog); QLineEdit *peopleEdit= new QLineEdit(&dialog); QLineEdit *phoneEdit= new QLineEdit(&dialog); // 添加控件到表单 formLayout->addRow("设备名称:", nameEdit); formLayout->addRow("设备类型:", typeEdit); formLayout->addRow("借出时间:", lend_out_dateEdit); formLayout->addRow("借用人:", peopleEdit); formLayout->addRow("手机·:", phoneEdit); // 创建按钮 QDialogButtonBox *buttonBox = new QDialogButtonBox( QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog); mainLayout->addLayout(formLayout); mainLayout->addWidget(buttonBox); // 连接按钮信号 connect(buttonBox, &QDialogButtonBox::accepted, &dialog, &QDialog::accept); connect(buttonBox, &QDialogButtonBox::rejected, &dialog, &QDialog::reject); // 显示对话框并处理结果 if (dialog.exec() == QDialog::Accepted) { // 获取输入值 QString name = nameEdit->text().trimmed(); QString type = typeEdit->text().trimmed(); QString date_1= lend_out_dateEdit->text().trimmed(); QString date_2= ""; QString status = "0"; QString people = peopleEdit->text().trimmed(); QString phone = phoneEdit->text().trimmed(); // 验证输入 if (name.isEmpty()) { QMessageBox::warning(this, "输入错误", "设备名不能为空"); return; } // 在模型末尾插入新行 int rowNum = model->rowCount(); model->insertRow(rowNum); // 设置新记录的值 model->setData(model->index(rowNum, model->fieldIndex("ID")), rowNum+1); model->setData(model->index(rowNum, model->fieldIndex("设备名称")), name); model->setData(model->index(rowNum, model->fieldIndex("设备类型")), type); model->setData(model->index(rowNum, model->fieldIndex("借还状态")), status); model->setData(model->index(rowNum, model->fieldIndex("借用时间")), date_1); model->setData(model->index(rowNum, model->fieldIndex("归还时间")), date_2); model->setData(model->index(rowNum, model->fieldIndex("借用人")),people); model->setData(model->index(rowNum, model->fieldIndex("手机")), phone); // 自动滚动到新添加的行 ui->tableView->scrollToBottom(); } } 为什么最终table view仅显示了ID
最新发布
07-09
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值