<QT>QDialog-->QMessageBox

//消息对话框为模态对话框

 QMessageBox 消息对话框有如下成员,

 

QMessageBox::question的第三个参数QMessageBox::StandardButton有如下枚举

 

 

 //消息对话框为模态对话框

    QMessageBox::about(this,"about","this is a 消息对话框!!");
    QMessageBox::critical(this,"error!","this is a 错误对话框!!");
    int ret = QMessageBox::question(this,"question","你要打开这个文件吗??",QMessageBox::Open,QMessageBox::Close);

    if(ret==QMessageBox::Open)
    {
        qDebug()<<"ret:"<<ret;
        QMessageBox::information(this,"inf","保存成功!!");

    }
    else if(ret==QMessageBox::Close)
    {
        QMessageBox::warning(this,"warning","错误!!!");

 

 

 

 

 

<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>PhoneDialog</class> <widget class="QDialog" name="PhoneDialog"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>300</height> </rect> </property> <property name="windowTitle"> <string>手机信息</string> </property> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>手机ID:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="idEdit"/> </item> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> <string>型号:</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="modelEdit"/> </item> <item row="2" column="0"> <widget class="QLabel" name="label_3"> <property name="text"> <string>品牌:</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="brandEdit"/> </item> <item row="3" column="0"> <widget class="QLabel" name="label_4"> <property name="text"> <string>进价:</string> </property> </widget> </item> <item row="3" column="1"> <widget class="QDoubleSpinBox" name="priceSpin"> <property name="decimals"> <number>2</number> </property> <property name="maximum"> <double>99999.000000000000000</double> </property> <property name="prefix"> <string>¥</string> </property> </widget> </item> <item row="4" column="0"> <widget class="QLabel" name="label_5"> <property name="text"> <string>库存数量:</string> </property> </widget> </item> <item row="4" column="1"> <widget class="QSpinBox" name="quantitySpin"> <property name="minimum"> <number>0</number> </property> <property name="maximum"> <number>9999</number> </property> </widget> </item> <item row="5" column="0" colspan="2"> <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="btnOK"> <property name="text"> <string>确定</string> </property> </widget> </item> <item> <widget class="QPushButton" name="btnCancel"> <property name="text"> <string>取消</string> </property> </widget> </item> </layout> </item> </layout> </widget> <resources/> <connections/> </ui> 根据上面的qt项目检查该ui代码
06-29
#pragma once #include "SERializableClassTest.h" #include <QJsonObject> #include <QJsonDocument> #include "MTFDataDefine.h" #include <QCoreApplication> // 定义数据类 class BasicTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool Time MEMBER Time) Q_PROPERTY(bool TrayName MEMBER TrayName) Q_PROPERTY(bool ConfigFile MEMBER ConfigFile) Q_PROPERTY(bool ThroughFocusData MEMBER ThroughFocusData) Q_PROPERTY(bool Position MEMBER Position) Q_PROPERTY(bool DriverPos MEMBER DriverPos) Q_PROPERTY(bool Defocus MEMBER Defocus) public: explicit BasicTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool Time; bool TrayName; bool ConfigFile; bool ThroughFocusData; bool Position; bool DriverPos; bool Defocus; }; class ResultTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool Final MEMBER Final) Q_PROPERTY(bool MTF MEMBER MTF) Q_PROPERTY(bool DOF MEMBER DOF) Q_PROPERTY(bool FocalShift MEMBER FocalShift) Q_PROPERTY(bool Astigmatism MEMBER Astigmatism) Q_PROPERTY(bool FOV MEMBER FOV) Q_PROPERTY(bool Average MEMBER Average) Q_PROPERTY(bool Aggregate MEMBER Aggregate) Q_PROPERTY(bool MultiFrqMTF MEMBER MultiFrqMTF) Q_PROPERTY(bool Defocus MEMBER Defocus) Q_PROPERTY(bool FailReason MEMBER FailReason) public: explicit ResultTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool Final; bool MTF; bool DOF; bool FocalShift; bool Astigmatism; bool FOV; bool Average; bool Aggregate; bool MultiFrqMTF; bool Defocus; bool FailReason; }; class TiltTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool Azimuth MEMBER Azimuth) Q_PROPERTY(bool Angle MEMBER Angle) Q_PROPERTY(bool Type MEMBER Type) public: explicit TiltTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool Azimuth; bool Angle; bool Type; }; class FocalShiftDataTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool Negative MEMBER Negative) Q_PROPERTY(bool Positive MEMBER Positive) Q_PROPERTY(bool Group MEMBER Group) Q_PROPERTY(bool Cameras MEMBER Cameras) public: explicit FocalShiftDataTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool Negative; bool Positive; bool Group; bool Cameras; }; class DOFDataTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool DOF MEMBER DOF) Q_PROPERTY(bool Minus MEMBER Minus) Q_PROPERTY(bool Plus MEMBER Plus) Q_PROPERTY(bool Cameras MEMBER Cameras) Q_PROPERTY(bool Group MEMBER Group) public: explicit DOFDataTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool DOF; bool Minus; bool Plus; bool Cameras; bool Group; }; class A2DOFDataTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool A2DOF MEMBER A2DOF) Q_PROPERTY(bool Minus MEMBER Minus) Q_PROPERTY(bool Plus MEMBER Plus) Q_PROPERTY(bool OnAxis MEMBER OnAxis) Q_PROPERTY(bool OffAxis MEMBER OffAxis) Q_PROPERTY(bool A2DOFAvg MEMBER A2DOFAvg) Q_PROPERTY(bool AvgMinus MEMBER AvgMinus) Q_PROPERTY(bool AvgPlus MEMBER AvgPlus) Q_PROPERTY(bool OnAxisAvg MEMBER OnAxisAvg) Q_PROPERTY(bool OffAxisAvg MEMBER OffAxisAvg) public: explicit A2DOFDataTree(QObject *parent = nullptr) : SerializableAssist(parent) {} public: bool A2DOF; bool Minus; bool Plus; //"Sag":"","Tan":"","ST":"" bool OnAxis; bool OffAxis; bool A2DOFAvg; bool AvgMinus; bool AvgPlus; bool OnAxisAvg; bool OffAxisAvg; }; class NormaltTree : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool EFL MEMBER EFL) Q_PROPERTY(bool FFL MEMBER FFL) Q_PROPERTY(bool MTF MEMBER MTF) Q_PROPERTY(bool MultiFrqMTF MEMBER MultiFrqMTF) Q_PROPERTY(bool MTFPeaks MEMBER MTFPeaks) Q_PROPERTY(bool HalfFOV MEMBER HalfFOV) Q_PROPERTY(TiltTree* Tilt MEMBER Tilt) Q_PROPERTY(FocalShiftDataTree* FocalShiftData MEMBER FocalShiftData) Q_PROPERTY(bool Astigmatism MEMBER Astigmatism) Q_PROPERTY(bool LSFPeaks MEMBER LSFPeaks) Q_PROPERTY(DOFDataTree* DOF MEMBER DOF) Q_PROPERTY(A2DOFDataTree* A2DOF MEMBER A2DOF) Q_PROPERTY(bool SymmetricalGroupMTF MEMBER SymmetricalGroupMTF) Q_PROPERTY(bool SymmetricalFieldMTF MEMBER SymmetricalFieldMTF) Q_PROPERTY(bool MTFAverageAllCameras MEMBER MTFAverageAllCameras) Q_PROPERTY(bool FieldPositionMTF MEMBER FieldPositionMTF) Q_PROPERTY(bool FocalShiftSingle MEMBER FocalShiftSingle) Q_PROPERTY(bool FocalShiftGroup MEMBER FocalShiftGroup) Q_PROPERTY(bool FocalShift MEMBER FocalShift) Q_PROPERTY(bool PeakRange MEMBER PeakRange) Q_PROPERTY(bool PeakMTF MEMBER PeakMTF) Q_PROPERTY(bool PeakMTFAverage MEMBER PeakMTFAverage) Q_PROPERTY(bool MTFRatio MEMBER MTFRatio) public: explicit NormaltTree(QObject *parent = nullptr) : SerializableAssist(parent) { Tilt = new TiltTree; FocalShiftData = new FocalShiftDataTree; DOF = new DOFDataTree; A2DOF = new A2DOFDataTree; } public: bool EFL; bool FFL; bool MTF; bool MultiFrqMTF; bool MTFPeaks; bool HalfFOV; bool FullFOV; TiltTree* Tilt; FocalShiftDataTree* FocalShiftData; bool Astigmatism; bool LSFPeaks; DOFDataTree* DOF; A2DOFDataTree* A2DOF; //(Max-Min) bool SymmetricalGroupMTF; //(S+T)/2(Max-Min) bool SymmetricalFieldMTF; //MTF Average of All Cameras bool MTFAverageAllCameras; //Field Position of MTF(S+T)/2 bool FieldPositionMTF; //Focal Shift Single (S+T)/2 bool FocalShiftSingle; //Focal Shift Group bool FocalShiftGroup; //Focal Shift (S+T)/2 bool FocalShift; //(Max-Min) bool PeakRange; //Peak MTF (Max-Min) bool PeakMTF; //Peak MTF Average bool PeakMTFAverage; //(Max - Min) / Max bool MTFRatio; }; class ConfigFileItem : public SerializableAssist { Q_OBJECT Q_PROPERTY(bool Barcode MEMBER Barcode) Q_PROPERTY(bool CameraCounts MEMBER CameraCounts) Q_PROPERTY(bool GroupDefine MEMBER GroupDefine) Q_PROPERTY(BasicTree* Basic MEMBER Basic) Q_PROPERTY(ResultTree* Result MEMBER Result) Q_PROPERTY(NormaltTree* Normal MEMBER Normal) Q_PROPERTY(bool Defocus MEMBER Defocus) public: explicit ConfigFileItem(QObject *parent = nullptr) : SerializableAssist(parent) { Basic = new BasicTree; Result = new ResultTree; Normal = new NormaltTree; } bool Barcode; bool CameraCounts; bool GroupDefine; BasicTree* Basic; ResultTree* Result; NormaltTree* Normal; bool Defocus; ~ConfigFileItem() { if (nullptr == Basic) { delete Basic; } if (nullptr == Result) { delete Result; } } ConfigFileItem(const ConfigFileItem &other) : SerializableAssist(other.parent()) { copyFrom(other); } ConfigFileItem& operator=(const ConfigFileItem &other) { if (this != &other) { copyFrom(other); } return *this; } private: void copyFrom(const ConfigFileItem &other) { Barcode = other.Barcode; CameraCounts = other.CameraCounts; GroupDefine = other.GroupDefine; Basic = other.Basic; Result = other.Result; Normal = other.Normal; Defocus = other.Defocus; } public: void CopyTestData(TestData &testData) { testData.m_bBarcode = this->Barcode; testData.m_bCameraCounts = this->CameraCounts ; testData.m_bGroupDefine = this->GroupDefine; testData.m_bDefocus = this->Defocus; { testData.Basic.m_bConfigFile = this->Basic->ConfigFile; testData.Basic.m_bDefocus = this->Basic->Defocus; testData.Basic.m_bDriverPos = this->Basic->DriverPos; testData.Basic.m_bPosition = this->Basic->Position; testData.Basic.m_bThroughFocusData = this->Basic->ThroughFocusData; testData.Basic.m_bTime = this->Basic->Time; testData.Basic.m_bTrayName = this->Basic->TrayName; } { testData.Normal.A2DOF.m_bA2DOF = this->Normal->A2DOF->A2DOF; testData.Normal.A2DOF.m_bA2DOFAvg = this->Normal->A2DOF->A2DOFAvg; testData.Normal.A2DOF.m_bAvgMinus = this->Normal->A2DOF->AvgMinus; testData.Normal.A2DOF.m_bAvgPlus = this->Normal->A2DOF->AvgPlus; testData.Normal.A2DOF.m_bMinus = this->Normal->A2DOF->Minus; testData.Normal.A2DOF.m_bOffAxis = this->Normal->A2DOF->OffAxis; testData.Normal.A2DOF.m_bOffAxisAvg = this->Normal->A2DOF->OffAxisAvg; testData.Normal.A2DOF.m_bOnAxis = this->Normal->A2DOF->OnAxis; testData.Normal.A2DOF.m_bOnAxisAvg = this->Normal->A2DOF->OnAxisAvg; testData.Normal.A2DOF.m_bPlus = this->Normal->A2DOF->Plus; testData.Normal.m_bAstigmatism = this->Normal->Astigmatism; testData.Normal.DOF.m_bDOF = this->Normal->DOF->DOF; testData.Normal.DOF.m_bCameras = this->Normal->DOF->Cameras; testData.Normal.DOF.m_bGroup = this->Normal->DOF->Group; testData.Normal.DOF.m_bMinus = this->Normal->DOF->Minus; testData.Normal.DOF.m_bPlus = this->Normal->DOF->Plus; testData.Normal.m_bEFL = this->Normal->EFL; testData.Normal.m_bFFL = this->Normal->FFL; testData.Normal.m_bFieldPositionMTF = this->Normal->FieldPositionMTF; testData.Normal.m_bFocalShift = this->Normal->FocalShift; testData.Normal.m_bFocalShiftGroup = this->Normal->FocalShiftGroup; testData.Normal.m_bFocalShiftSingle = this->Normal->FocalShiftSingle; testData.Normal.m_bHalfFOV = this->Normal->HalfFOV; testData.Normal.m_bFullFOV = this->Normal->FullFOV; testData.Normal.m_bLSFPeaks = this->Normal->LSFPeaks; testData.Normal.m_bMTF = this->Normal->MTF; testData.Normal.m_bMTFAverageAllCameras = this->Normal->MTFAverageAllCameras; testData.Normal.m_bMTFPeaks = this->Normal->MTFPeaks; testData.Normal.m_bMTFRatio = this->Normal->MTFRatio; testData.Normal.m_bMultiFrqMTF = this->Normal->MultiFrqMTF; testData.Normal.m_bPeakMTF = this->Normal->PeakMTF; testData.Normal.m_bPeakMTFAverage = this->Normal->PeakMTFAverage; testData.Normal.m_bPeakRange = this->Normal->PeakRange; testData.Normal.m_bSymmetricalFieldMTF = this->Normal->SymmetricalFieldMTF; testData.Normal.m_bSymmetricalGroupMTF = this->Normal->SymmetricalGroupMTF; testData.Normal.Tilt.m_bAngle = this->Normal->Tilt->Angle; testData.Normal.Tilt.m_bType = this->Normal->Tilt->Type; testData.Normal.Tilt.m_bAzimuth = this->Normal->Tilt->Azimuth; testData.Normal.FocalShift.m_bCameras = this->Normal->FocalShiftData->Cameras; testData.Normal.FocalShift.m_bGroup = this->Normal->FocalShiftData->Group; testData.Normal.FocalShift.m_bNegative = this->Normal->FocalShiftData->Negative; testData.Normal.FocalShift.m_bPositive = this->Normal->FocalShiftData->Positive; testData.Result.m_bFinal = this->Result->Final; testData.Result.m_bMTF = this->Result->MTF; testData.Result.m_bDOF = this->Result->DOF; testData.Result.m_bFocalShift = this->Result->FocalShift; testData.Result.m_bAstigmatism = this->Result->Astigmatism; testData.Result.m_bFOV = this->Result->FOV; testData.Result.m_bAverage = this->Result->Average; testData.Result.m_bAggregate = this->Result->Aggregate; testData.Result.m_bMultiFrqMTF = this->Result->MultiFrqMTF; testData.Result.m_bDefocus = this->Result->Defocus; testData.Result.m_bFailReason = this->Result->FailReason; } } }; #pragma once #include <qdialog.h> #include <QTreeWidget> #include <QFileDialog> #include <QJsonDocument> #include <QJsonArray> #include <QJsonObject> #include <QFile> #include <QVBoxLayout> #include <QDebug> #include <QCoreApplication> #include <QPushButton> #include <QMessageBox> #include <QStandardItemModel> #include <QStyledItemDelegate> #include "JsonAssist.h" #include "ConfigFileItem.h" class ConfigFileQTreeWidget :public QDialog { Q_OBJECT public: ConfigFileQTreeWidget(QWidget *parent = nullptr); ~ConfigFileQTreeWidget(); private: bool loadJsonFile(const QString& filePath); bool loadFile(); void saveJsonFile(); // 递归函数:将 JSON 对象转换为 QStandardItemModel 的树形结构 void addJsonObjectToModel(const QJsonObject &obj, QStandardItem *parentItem); const QJsonObject modelToJson(QStandardItem *parentItem); private: QStandardItemModel *model; QTreeView *treeView; QPushButton *saveButton; QPushButton *LoadButton; QString configPath; QJsonObject rootObj; }; 这两个怎么用?
08-26
<?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWnd</class> <widget class="QMainWindow" name="MainWnd"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>400</width> <height>120</height> </rect> </property> <property name="minimumSize"> <size> <width>400</width> <height>120</height> </size> </property> <property name="windowTitle"> <string>FTPClient</string> </property> <widget class="QWidget" name="centralWidget"> <layout class="QGridLayout" name="gridLayout"> <item row="0" column="1"> <widget class="QProgressBar" name="pgbUpload"> <property name="minimumSize"> <size> <width>0</width> <height>23</height> </size> </property> <property name="value"> <number>0</number> </property> </widget> </item> <item row="1" column="1"> <widget class="QProgressBar" name="pgbDownload"> <property name="minimumSize"> <size> <width>0</width> <height>23</height> </size> </property> <property name="value"> <number>0</number> </property> </widget> </item> <item row="1" column="0"> <widget class="QToolButton" name="tbDownload"> <property name="minimumSize"> <size> <width>80</width> <height>23</height> </size> </property> <property name="text"> <string>下载</string> </property> </widget> </item> <item row="0" column="0"> <widget class="QToolButton" name="tbUpload"> <property name="minimumSize"> <size> <width>80</width> <height>23</height> </size> </property> <property name="text"> <string>上传</string> </property> </widget> </item> <item row="0" column="2"> <widget class="QPushButton" name="upOnOffBt"> <property name="text"> <string>暂停</string> </property> </widget> </item> <item row="1" column="2"> <widget class="QPushButton" name="dlOnOffBt"> <property name="text"> <string>暂停</string> </property> </widget> </item> </layout> </widget> <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/> </ui> 根据这个ui来生成使用QNetworkAccessManager来断点续传上传下载文件的代码。
10-13
#include "mainwindow.h" #include "ui_mainwindow.h" #include "adddialog.h" #include <QMenu> #include <QAction> #include <QCursor> #include <QMessageBox> #include <QPushButton> #include <QSqlDatabase> #include <QSqlQuery> #include <QDebug> #include <QSqlError> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) , dataManager(DataManager::getInstance()) { ui->setupUi(this); initTreeWidget(); initTableWidget(); connect(ui->treeWidget, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(onTreeItemClicked(QTreeWidgetItem*, int))); connect(ui->treeWidget,SIGNAL(customContextMenuRequested(QPoint)), this,SLOT(ContextMenuRequested(QPoint))); } MainWindow::~MainWindow() { delete ui; } void MainWindow::initTreeWidget() { ui->treeWidget->setContextMenuPolicy(Qt::CustomContextMenu); ui->treeWidget->setHeaderLabel("学生管理系统"); QTreeWidgetItem *uitem=new QTreeWidgetItem(); uitem->setText(0,"北京大学"); uitem->setData(0, Qt::UserRole, 0); ui->treeWidget->addTopLevelItem(uitem); QTreeWidgetItem *citem=new QTreeWidgetItem(); citem->setText(0,"计算机学院"); citem->setData(0, Qt::UserRole, 1); uitem->addChild(citem); QTreeWidgetItem *titem=new QTreeWidgetItem(); titem->setText(0,"老师"); titem->setData(0, Qt::UserRole, 2); citem->addChild(titem); QTreeWidgetItem *sitem=new QTreeWidgetItem(); sitem->setText(0,"学生"); sitem->setData(0, Qt::UserRole, 3); citem->addChild(sitem); QTreeWidgetItem *titem1=new QTreeWidgetItem(); titem1->setText(0,"闻清砚"); titem1->setData(0, Qt::UserRole, 4); titem->addChild(titem1); QTreeWidgetItem *sitem1=new QTreeWidgetItem(); sitem1->setText(0,"司南钰"); sitem1->setData(0, Qt::UserRole, 5); sitem->addChild(sitem1 ); ui->treeWidget->expandAll(); } void MainWindow::initTableWidget() { QStringList headers = {"类型", "ID", "姓名", "年龄", "身高"}; ui->tableWidget->setColumnCount(headers.size()); ui->tableWidget->setHorizontalHeaderLabels(headers); } void MainWindow::ContextMenuRequested(QPoint pos) { if(ui->treeWidget->itemAt(pos) == nullptr) { //当点击的位置没有条目 //显示菜单 QMenu menu; QAction *addact = new QAction; addact->setText("add University"); menu.addAction(addact); connect(addact,SIGNAL(triggered(bool)), this,SLOT(addUniversity())); QAction *removeact = new QAction; removeact->setText("remove all"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)), this,SLOT(removeAll())); menu.exec(QCursor::pos()); }else { //当点击的位置有条目 QMenu menu; if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 0) { QAction *addact = new QAction; addact->setText("add Collage"); menu.addAction(addact); connect(addact,SIGNAL(triggered(bool)), this,SLOT(addCollage())); menu.exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 1) { QAction *stuaddact = new QAction; stuaddact->setText("add student"); menu.addAction(stuaddact); connect(stuaddact,SIGNAL(triggered(bool)),this,SLOT(addStudent())); QAction *teaaddact = new QAction; teaaddact->setText("add teacher"); menu.addAction(teaaddact); connect(teaaddact,SIGNAL(triggered(bool)),this,SLOT(addTeacher())); QAction *removeact = new QAction; removeact->setText("remove Collage"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)),this,SLOT(removeCollage())); menu.exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 2) { QAction *teaaddact = new QAction; teaaddact->setText("add teachername"); menu.addAction(teaaddact); connect(teaaddact,SIGNAL(triggered(bool)),this,SLOT(addTeacherName())); QAction *removeact = new QAction; removeact->setText("remove Teacher"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)), this,SLOT(removeTeacher())); menu.exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 3) { QAction *stuaddact = new QAction; stuaddact->setText("add studentname"); menu.addAction(stuaddact); connect(stuaddact,SIGNAL(triggered(bool)),this,SLOT(addStudentName())); QAction *removeact = new QAction; removeact->setText("remove Student"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)), this,SLOT(removeStudent())); menu.exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 4) { QAction *removeact = new QAction; removeact->setText("remove TeacherName"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)), this,SLOT(removeTeacherName())); menu.exec(QCursor::pos()); } else if(ui->treeWidget->currentItem()->data(0,Qt::UserRole) == 5) { QAction *removeact = new QAction; removeact->setText("remove StudentName"); menu.addAction(removeact); connect(removeact,SIGNAL(triggered(bool)), this,SLOT(removeStudentName())); menu.exec(QCursor::pos()); } if (!menu.actions().isEmpty()) { menu.exec(ui->treeWidget->mapToGlobal(pos)); } } } void MainWindow::addUniversity() { //添加根节点 QTreeWidgetItem *uitem = new QTreeWidgetItem; //设置隐藏属性 uitem->setData(0,Qt::UserRole,0); uitem->setText(0,"学校"); ui->treeWidget->addTopLevelItem(uitem); } */ void MainWindow::addCollage() { /*//1.找到当前节点 QTreeWidgetItem*cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem; child->setData(0,Qt::UserRole,1); child->setText(0,"学院"); cur->addChild(child); cur->setExpanded(true); // 自动展开父节点 */ AddDialog dialog(AddDialog::College, this); if (dialog.exec() == QDialog::Accepted) { QString cid = dialog.getText1(); QString cname = dialog.getText2(); if (cid.isEmpty() || cname.isEmpty()) return; QSqlQuery query; query.prepare("INSERT INTO College (cid, cname, uid) VALUES (?, ?, ?)"); query.addBindValue(cid); query.addBindValue(cname); query.addBindValue(dataManager->getUniversityUid()); if (query.exec()) { QTreeWidgetItem *cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem(); child->setText(0, cname); child->setData(0, Qt::UserRole, 1); // 类型为学院 cur->addChild(child); cur->setExpanded(true); } else { QMessageBox::warning(this, "错误", "无法添加学院:" + query.lastError().text()); } } } void MainWindow::addTeacher() { //1.找到当前节点 QTreeWidgetItem*cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem; child->setData(0,Qt::UserRole,2); child->setText(0,"老师"); cur->addChild(child); cur->setExpanded(true); } void MainWindow::addStudent() { //1.找到当前节点 QTreeWidgetItem*cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem; child->setData(0,Qt::UserRole,3); child->setText(0,"学生"); cur->addChild(child); cur->setExpanded(true); // 自动展开父节点 } void MainWindow::removeAll() { //删除所有节点 ui->treeWidget->clear(); } void MainWindow::removeCollage() { QTreeWidgetItem * paret = ui->treeWidget->currentItem()->parent(); paret->takeChild(ui->treeWidget->currentIndex().row()); } void MainWindow::addTeacherName() { //1.找到当前节点 QTreeWidgetItem*cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem; child->setData(0,Qt::UserRole,4); child->setText(0,"老师1"); cur->addChild(child); cur->setExpanded(true); } void MainWindow::addStudentName() { //1.找到当前节点 QTreeWidgetItem*cur = ui->treeWidget->currentItem(); QTreeWidgetItem *child = new QTreeWidgetItem; child->setData(0,Qt::UserRole,5); child->setText(0,"学生1"); cur->addChild(child); cur->setExpanded(true); } void MainWindow::removeTeacher() { QTreeWidgetItem * parent = ui->treeWidget->currentItem()->parent(); parent->takeChild(ui->treeWidget->currentIndex().row()); } void MainWindow::removeStudent() { QTreeWidgetItem * parent = ui->treeWidget->currentItem()->parent(); parent->takeChild(ui->treeWidget->currentIndex().row()); } void MainWindow::removeTeacherName() { //QTreeWidgetItem * parent = ui->treeWidget->currentItem()->parent(); // parent->takeChild(ui->treeWidget->currentIndex().row()); QTreeWidgetItem *item = ui->treeWidget->currentItem(); if (!item || item->data(0, Qt::UserRole).toInt() != 4) return; QString name = item->text(0); QSqlQuery q; q.prepare("DELETE FROM Teacher WHERE name = ?"); q.addBindValue(name); if (q.exec()) { delete item; // 成功则删除 UI 节点 } else { QMessageBox::warning(this, "删除失败", q.lastError().text()); } } void MainWindow::removeStudentName() { //QTreeWidgetItem * parent = ui->treeWidget->currentItem()->parent(); //parent->takeChild(ui->treeWidget->currentIndex().row()); QTreeWidgetItem *item = ui->treeWidget->currentItem(); if (!item || item->data(0, Qt::UserRole).toInt() != 5) return; QString name = item->text(0); QSqlQuery q; q.prepare("DELETE FROM Teacher WHERE name = ?"); q.addBindValue(name); if (q.exec()) { delete item; // 成功则删除 UI 节点 } else { QMessageBox::warning(this, "删除失败", q.lastError().text()); } } void MainWindow::onTreeItemClicked(QTreeWidgetItem *item, int column) { Q_UNUSED(column) if (!item) return; int role = item->data(0, Qt::UserRole).toInt(); QString text = item->text(0); switch (role) { case 0: showUniversityInfo(); break; case 1: showCollegeInfo(item); break; case 4: showTeacherInfo(text); break; case 5: showStudentInfo(text); break; /*case 2: { // 展开该学院的所有老师 updateTeacherGroup(item->parent()); break; } case 3: { // 展开该学院的所有学生 updateStudentGroup(item->parent()); break; } */ } } void MainWindow::showUniversityInfo() { //ui->tableWidget->clear(); QStringList headers = {"大学ID", "大学名称", "地址"}; ui->tableWidget->setColumnCount(3); ui->tableWidget->setHorizontalHeaderLabels(headers); ui->tableWidget->setRowCount(1); ui->tableWidget->setItem(0, 0, new QTableWidgetItem(dataManager->getUniversityUid())); ui->tableWidget->setItem(0, 1, new QTableWidgetItem(dataManager->getUniversityUname())); ui->tableWidget->setItem(0, 2, new QTableWidgetItem(dataManager->getUniversityUaddr())); } void MainWindow::showCollegeInfo(QTreeWidgetItem *item) { QString cid = item->text(0); // 实际应从父级获取 ID const Collage* c = dataManager->getCollegeById(cid); if (!c) return; ui->tableWidget->clearContents(); ui->tableWidget->setRowCount(1); ui->tableWidget->setItem(0, 0, new QTableWidgetItem("学院")); ui->tableWidget->setItem(0, 1, new QTableWidgetItem(QString::fromStdString(c->cid()))); ui->tableWidget->setItem(0, 2, new QTableWidgetItem(QString::fromStdString(c->cname()))); } void MainWindow::showTeacherInfo(const QString& name) { const Teacher* t = nullptr; auto uni = University::getInstance(); for (const auto& c : uni->c_list) { for (const auto& teacher : c->t_list) { if (QString::fromStdString(teacher->name()) == name) { t = teacher; break; } } if (t) break; } if (!t) return; ui->tableWidget->clearContents(); ui->tableWidget->setRowCount(1); ui->tableWidget->setItem(0, 0, new QTableWidgetItem("教师")); ui->tableWidget->setItem(0, 1, new QTableWidgetItem(QString::fromStdString(t->tid()))); ui->tableWidget->setItem(0, 2, new QTableWidgetItem(QString::fromStdString(t->name()))); ui->tableWidget->setItem(0, 3, new QTableWidgetItem(QString::number(t->age()))); ui->tableWidget->setItem(0, 4, new QTableWidgetItem(QString::number(t->height()))); } void MainWindow::showStudentInfo(const QString& name) { const Student* s = nullptr; auto uni = University::getInstance(); for (const auto& c : uni->c_list) { for (const auto& student : c->s_list) { if (QString::fromStdString(student->name()) == name) { s = student; break; } } if (s) break; } if (!s) return; ui->tableWidget->clearContents(); ui->tableWidget->setRowCount(1); ui->tableWidget->setItem(0, 0, new QTableWidgetItem("学生")); ui->tableWidget->setItem(0, 1, new QTableWidgetItem(QString::fromStdString(s->sid()))); ui->tableWidget->setItem(0, 2, new QTableWidgetItem(QString::fromStdString(s->name()))); ui->tableWidget->setItem(0, 3, new QTableWidgetItem(QString::number(s->age()))); ui->tableWidget->setItem(0, 4, new QTableWidgetItem(QString::number(s->height()))); } 在原代码中略微修改
最新发布
12-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-breezeyy-

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值