From aaac66e4a77c7d0b91e6fdcb849d284ee57fa810 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 6 Jul 2022 17:36:11 +0800 Subject: [PATCH] added a new Dialog it's NameEdit --- inc/hgl/qt/dialog/NameEdit.h | 38 +++++++++++++++ src/CMakeLists.txt | 9 +++- src/dialog/NameEdit.cpp | 89 ++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 2 deletions(-) create mode 100644 inc/hgl/qt/dialog/NameEdit.h create mode 100644 src/dialog/NameEdit.cpp diff --git a/inc/hgl/qt/dialog/NameEdit.h b/inc/hgl/qt/dialog/NameEdit.h new file mode 100644 index 0000000..afb0735 --- /dev/null +++ b/inc/hgl/qt/dialog/NameEdit.h @@ -0,0 +1,38 @@ +#pragma once + +#include +#include +#include + +namespace hgl +{ + namespace qt + { + namespace dialog + { + class NameEdit:public QDialog + { + Q_OBJECT + + protected: + + QLineEdit *name_edit; + + QLabel *hint_label; + + void OnCancelClicked(); + + protected: + + virtual bool OnNameCheck(QString &)=0; + + public: + + NameEdit(const QString &,const QString &); + virtual ~NameEdit()=default; + + virtual void OnOKClicked(); + };//class NameEdit:public QDialog + }//namespace dialog + }//namespace qt +}//namespace hgl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 94d41eb..2edcd80 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -11,7 +11,12 @@ ELSEIF() set(CMQT_MAIN_SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/QT5UnixUTF8.cpp) endif() -add_cm_library(CMQT "CM" QT5GuiApplication.cpp) +SET(CMQT_DIALOG_FILES ${CMQT_DIALOG_INCLUDE_PATH}/NameEdit.h + dialog/NameEdit.cpp) -SET(CMQT_LIBRARIES CMQT Qt5::Core Qt5::Gui Qt5::Widgets) +SOURCE_GROUP("Dialog" FILES ${CMQT_DIALOG_FILES}) + +add_cm_library(CMQT "CM" ${CMQT_DIALOG_FILES}) + +target_link_libraries(CMQT PUBLIC Qt5::Core Qt5::Gui Qt5::Widgets) diff --git a/src/dialog/NameEdit.cpp b/src/dialog/NameEdit.cpp new file mode 100644 index 0000000..620bcb3 --- /dev/null +++ b/src/dialog/NameEdit.cpp @@ -0,0 +1,89 @@ +#include +#include +#include + +namespace hgl +{ + namespace qt + { + namespace dialog + { + NameEdit::NameEdit(const QString &hint_text,const QString &str) + { + setModal(true); + + QVBoxLayout *layout=new QVBoxLayout(this); + + { + QLabel *lab=new QLabel(hint_text,this); + + lab->setAlignment(Qt::AlignLeft); + + layout->addWidget(lab); + } + + { + name_edit=new QLineEdit(str,this); + + name_edit->setAlignment(Qt::AlignCenter); + + name_edit->setCursorPosition(0); + + layout->addWidget(name_edit); + } + + { + hint_label=new QLabel(this); + + hint_label->setAlignment(Qt::AlignCenter); + + layout->addWidget(hint_label); + } + + //按钮区 + { + QWidget *button_widget=new QWidget(this); + QHBoxLayout *button_layout=new QHBoxLayout(button_widget); + + //确定、取消按钮 + { + QPushButton *ok_button=new QPushButton(button_widget); + ok_button->setText(tr("OK")); + connect(ok_button,&QPushButton::clicked,this,&NameEdit::OnOKClicked); + + button_layout->addWidget(ok_button,0,Qt::AlignRight); + + QPushButton *cancel_button=new QPushButton(button_widget); + cancel_button->setText(tr("Cancel")); + connect(cancel_button,&QPushButton::clicked,this,&NameEdit::OnCancelClicked); + + button_layout->addWidget(cancel_button,0,Qt::AlignLeft); + } + + layout->addWidget(button_widget); + } + + resize(layout->sizeHint()); + } + + void NameEdit::OnCancelClicked() + { + close(); + } + + void NameEdit::OnOKClicked() + { + QString err_text; + + if(OnNameCheck(err_text)) + close(); + + QPalette palette=hint_label->palette(); + palette.setColor(hint_label->foregroundRole(),Qt::GlobalColor::red); + + hint_label->setPalette(palette); + hint_label->setText(err_text); + } + }//namespace dialog + }//namespace qt +}//namespace hgl