diff --git a/CMakeLists.txt b/CMakeLists.txt index b3a59a4..fc0b1da 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,7 +14,7 @@ find_package(Qt5 COMPONENTS Widgets REQUIRED) -add_executable(officeassistant main.cpp mainwindow.cpp mainwindow.h mainwindow.ui mainwindowlayout.cpp mainwindowlayout.h mainwindowlayout.ui navbar.cpp navbar.h navbar.ui MyButton.cpp MyButton.h buttonstruct.h) +add_executable(officeassistant WIN32 main.cpp mainwindow.cpp mainwindow.h mainwindow.ui mainwindowlayout.cpp mainwindowlayout.h mainwindowlayout.ui navbar.cpp navbar.h navbar.ui MyButton.cpp MyButton.h buttonstruct.h globalvariables.h mainscreen.cpp mainscreen.h mainscreen.ui) target_link_libraries(officeassistant Qt5::Core Qt5::Gui diff --git a/MyButton.cpp b/MyButton.cpp index 73b49ea..83aacff 100644 --- a/MyButton.cpp +++ b/MyButton.cpp @@ -1,15 +1,16 @@ // // Created by HW on 2023/07/26. // - +#define TEXT_SIZE 7 #include "MyButton.h" -MyButton::MyButton(QString logo,QString text,int width,int height,QWidget *parent) : QPushButton(parent) { +MyButton::MyButton(QString logo,QString text,int width,int height,QList *buttons,QWidget *parent) : QPushButton(parent) { + this->buttons=buttons; this->width2=width; this->height2=height; this->text=text; this->logo=new QImage; this->logo->load(logo); - this->setStyleSheet("background-color:#333332;"); + this->setStyleSheet("background-color:#333332;/*border:none;*/"); } MyButton::~MyButton() { @@ -20,15 +21,52 @@ void MyButton::paintEvent(QPaintEvent *e) { QPushButton::paintEvent(e); QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); - int logo_width=width()/2; - int logo_x=width()/4; + int logo_width=width()/3; + int logo_x=width()/3; + int logo_y=(height()-logo_width)/3; + setContentsMargins(0,0,0,0); QFont ft; - ft.setPixelSize(7); - int text_x=0; - int text_y=(logo_width+logo_x); - QRectF logo_rect(logo_x, logo_x, logo_width, logo_width); - QRectF text_rect(text_x,text_y,7*text.length(),7); + ft.setPixelSize(TEXT_SIZE); + int text_x=(width()-TEXT_SIZE*text.length()*scale*2)/2; + int text_y=(logo_width+logo_y+10); + QRectF logo_rect(logo_x, logo_y, logo_width, logo_width); + QRectF text_rect(text_x,text_y, TEXT_SIZE*text.length()*scale*2,TEXT_SIZE*scale*2); painter.setPen(QColor("#FFFFFF")); painter.drawImage(logo_rect, *logo); painter.drawText(text_rect, Qt::AlignCenter, text); + if(checked){ + QRectF rect(0, this->height()-6, this->width(), 5); + painter.fillRect(rect, QColor("#FFFFFF")); + } + } +QSize MyButton::sizeHint() const { + return QSize(width2,height2); +} + +void MyButton::enterEvent(QEvent *event) { + QWidget::enterEvent(event); + this->setStyleSheet("background-color:#646464;border:none;"); +} + +void MyButton::leaveEvent(QEvent *event) { + QWidget::leaveEvent(event); + this->setStyleSheet("background-color:#333332;border:none;"); +} + +void MyButton::mousePressEvent(QMouseEvent *event) { + QPushButton::mousePressEvent(event); +} + +void MyButton::mouseReleaseEvent(QMouseEvent *event) { + QPushButton::mouseReleaseEvent(event); + for(auto button:*buttons){ + button->setChecked(false); + button->update(); + } + if(checked) + checked=false; + else + checked=true; +} + diff --git a/MyButton.h b/MyButton.h index fe83337..402690d 100644 --- a/MyButton.h +++ b/MyButton.h @@ -10,21 +10,42 @@ #include #include #include +#include "globalvariables.h" class MyButton: public QPushButton{ public: - MyButton(QString logo,QString text,int width,int height,QWidget *parent=nullptr); + MyButton(QString logo,QString text,int width,int height,QList *buttons,QWidget *parent=nullptr); ~MyButton() override; QString getText(){ return text; } - QSize sizeHint() const override { - return QSize(width2/2,height2/2); - } + QSize sizeHint() const override; + void setChecked(bool checked){ + this->checked=checked; + } + QSize getLogicalSize() const { + if(scale>=1.25){ + return QSize(width2,height2); + } + else{ + return QSize(width2,height2); + }; + } + int setSize(int width,int height,int logicalwidth,int logicalheight){ + this->width2=width; + this->height2=height; + this->logicalwidth=logicalwidth; + this->logicalheight=logicalheight; + } protected: void paintEvent(QPaintEvent *e) override; + void enterEvent(QEvent *event) override; + void leaveEvent(QEvent *event) override; + void mousePressEvent(QMouseEvent *event) override; + void mouseReleaseEvent(QMouseEvent *event) override; private: + QList *buttons; QImage *logo; QLabel *logo_label; QLabel *text_label; @@ -33,6 +54,9 @@ private: QString text; int width2; int height2; + int logicalwidth; + int logicalheight; + bool checked=false; }; diff --git a/globalvariables.h b/globalvariables.h new file mode 100644 index 0000000..88a728f --- /dev/null +++ b/globalvariables.h @@ -0,0 +1,8 @@ +// +// Created by HW on 2023/07/27. +// + +#ifndef OFFICEASSISTANT_GLOBALVARIABLES_H +#define OFFICEASSISTANT_GLOBALVARIABLES_H +extern double scale; +#endif //OFFICEASSISTANT_GLOBALVARIABLES_H diff --git a/main.cpp b/main.cpp index 7ba5a95..d9a30cf 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,14 @@ #include "mainwindow.h" #include - +#include +double scale; int main(int argc, char *argv[]) { - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QApplication a(argc, argv); + HDC hdc = GetDC(NULL); + scale = GetDeviceCaps(hdc, LOGPIXELSX)/96; + ReleaseDC(NULL,hdc); MainWindow w; w.show(); return a.exec(); diff --git a/mainscreen.cpp b/mainscreen.cpp new file mode 100644 index 0000000..0d5db32 --- /dev/null +++ b/mainscreen.cpp @@ -0,0 +1,19 @@ +// +// Created by HW on 2023/07/27. +// + +// You may need to build the project (run Qt uic code generator) to get "ui_MainScreen.h" resolved + +#include "mainscreen.h" +#include "ui_MainScreen.h" + + +MainScreen::MainScreen(QWidget *parent) : + QWidget(parent), ui(new Ui::MainScreen) { + ui->setupUi(this); + +} + +MainScreen::~MainScreen() { + delete ui; +} diff --git a/mainscreen.h b/mainscreen.h new file mode 100644 index 0000000..e1f09f0 --- /dev/null +++ b/mainscreen.h @@ -0,0 +1,28 @@ +// +// Created by HW on 2023/07/27. +// + +#ifndef OFFICEASSISTANT_MAINSCREEN_H +#define OFFICEASSISTANT_MAINSCREEN_H + +#include + + +QT_BEGIN_NAMESPACE +namespace Ui { class MainScreen; } +QT_END_NAMESPACE + +class MainScreen : public QWidget { +Q_OBJECT + +public: + explicit MainScreen(QWidget *parent = nullptr); + + ~MainScreen() override; + +private: + Ui::MainScreen *ui; +}; + + +#endif //OFFICEASSISTANT_MAINSCREEN_H diff --git a/mainscreen.ui b/mainscreen.ui new file mode 100644 index 0000000..18d5e74 --- /dev/null +++ b/mainscreen.ui @@ -0,0 +1,22 @@ + + + + + + MainScreen + + + + 0 + 0 + 400 + 300 + + + + MainScreen + + + + + \ No newline at end of file diff --git a/mainwindow.cpp b/mainwindow.cpp index 7431a92..28b8cd7 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -14,18 +14,21 @@ MainWindow::MainWindow(QWidget *parent) : ui->setupUi(this); QDesktopWidget* desktopWidget = QApplication::desktop(); QRect deskRect = desktopWidget->availableGeometry(); - if(deskRect.width()>=1920){ - resize(1920,1080); - }else if(deskRect.width()>=1333&&deskRect.width()<=1920) { - resize(800, 540); - } delete desktopWidget; mainWindowLayout=new MainWindowLayout(this); setCentralWidget(mainWindowLayout); + if(deskRect.width()*scale>1920){ + resize(1600,1080); + this->setMaximumWidth(1600); + }else{ + resize(800, 540); + this->setMaximumWidth(800); + } + + } MainWindow::~MainWindow() { delete mainWindowLayout; - delete layout; delete ui; } diff --git a/mainwindowlayout.cpp b/mainwindowlayout.cpp index 5e92b2f..42d6e65 100644 --- a/mainwindowlayout.cpp +++ b/mainwindowlayout.cpp @@ -25,13 +25,17 @@ MainWindowLayout::MainWindowLayout(QWidget *parent) : buttons[3].text="个人中心"; buttons[3].logo=".\\images\\personalcenter.png"; buttons[3].index=3; - for(auto i=0;i<4;i++){ - list<<&buttons[i]; + for(auto i=0;i<4;i++) { + list << &buttons[i]; } + this->setLayout(nullptr); navBar=new NavBar(list,this); - layout->addWidget(navBar,1); - layout->addStretch(4); + mainScreen=new MainScreen(); + //QSizePolicy sizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::QSizePolicy::Fixed); + //navBar->setSizePolicy(sizePolicy); layout->setMargin(0); + layout->addWidget(navBar,1); + layout->addWidget(mainScreen,4); setLayout(layout); } @@ -39,6 +43,21 @@ MainWindowLayout::~MainWindowLayout() { delete[] list[0]; delete navBar; - delete layout; + //delete layout; delete ui; } + +void MainWindowLayout::resizeEvent(QResizeEvent *event) { + QWidget::resizeEvent(event); + navBar->resize(navBar->width(),height()/5); + //layout->setAlignment(Qt::AlignmentFlag::AlignHCenter); +} + +/*void MainWindowLayout::paintEvent(QPaintEvent *event) { + QWidget::paintEvent(event); + QPainter painter(this); + painter.setRenderHint(QPainter::Antialiasing); + + QRectF rect(0, 0, this->width(), this->height()/5); + painter.fillRect(rect, QColor("#333332")); +}*/ diff --git a/mainwindowlayout.h b/mainwindowlayout.h index a2f584a..f1421e9 100644 --- a/mainwindowlayout.h +++ b/mainwindowlayout.h @@ -9,6 +9,7 @@ #include #include #include "navbar.h" +#include "mainscreen.h" QT_BEGIN_NAMESPACE @@ -22,6 +23,9 @@ public: explicit MainWindowLayout(QWidget *parent = nullptr); ~MainWindowLayout() override; +protected: + void resizeEvent(QResizeEvent *event) override; + //void paintEvent(QPaintEvent *event) override; private: Ui::MainWindowLayout *ui; @@ -29,6 +33,7 @@ private: QList list; NavBar *navBar; ButtonStruct *buttons; + MainScreen *mainScreen; }; diff --git a/navbar.cpp b/navbar.cpp index c8ac2cd..8f6fa13 100644 --- a/navbar.cpp +++ b/navbar.cpp @@ -12,18 +12,19 @@ NavBar::NavBar(QList &buttonNames,QWidget *parent) : QWidget(parent), ui(new Ui::NavBar) { ui->setupUi(this); layout=new QHBoxLayout(); - layout->setMargin(0); + layout->setContentsMargins(0,0,0,0); this->setMinimumHeight(48); - this->setMaximumHeight(150); + this->setMaximumHeight(200); for(auto buttonName : buttonNames){ - MyButton *button=new MyButton(buttonName->logo,buttonName->text,height(),height()); - //QSizePolicy sizePolicy(QSizePolicy::Policy::Maximum, QSizePolicy::QSizePolicy::Maximum); - //button->setSizePolicy(sizePolicy); + MyButton *button=new MyButton(buttonName->logo,buttonName->text,height(),height(),&buttons); + QSizePolicy sizePolicy(QSizePolicy::Policy::Fixed, QSizePolicy::QSizePolicy::Fixed); + button->setSizePolicy(sizePolicy); layout->addWidget(button); buttons<setAlignment(Qt::AlignHCenter); + layout->setAlignment(Qt::AlignHCenter); + layout->setSpacing(0); this->setLayout(layout); } @@ -44,4 +45,13 @@ void NavBar::paintEvent(QPaintEvent *event) { QRectF rect(0, 0, this->width(), this->height()); painter.fillRect(rect, QColor("#333332")); + +} +void NavBar::resizeEvent(QResizeEvent *event) { + QWidget::resizeEvent(event); + for(auto button:buttons){ + button->setSize(event->size().height(),event->size().height(),event->size().height(),event->size().height()); + button->resize(event->size().height(),event->size().height()); + button->update(); + } } diff --git a/navbar.h b/navbar.h index 0dc8ef8..941e78b 100644 --- a/navbar.h +++ b/navbar.h @@ -23,11 +23,17 @@ public: ~NavBar() override; protected: - void paintEvent(QPaintEvent *event); + void paintEvent(QPaintEvent *event) override; + + void resizeEvent(QResizeEvent *event) override; + + private: Ui::NavBar *ui; QHBoxLayout *layout; QList buttons; + int width2; + int height2; };