界面完成

master
Mike Solar 2023-07-27 13:44:54 +08:00
parent 25daaecbc0
commit b262eca43c
13 changed files with 220 additions and 35 deletions

View File

@ -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

View File

@ -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<MyButton *> *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;
}

View File

@ -10,21 +10,42 @@
#include <QtSvg/QtSvg>
#include <QtSvg/QSvgWidget>
#include <QtSvg/QSvgRenderer>
#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<MyButton *> *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<MyButton *> *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;
};

8
globalvariables.h Normal file
View File

@ -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

View File

@ -1,11 +1,14 @@
#include "mainwindow.h"
#include <QApplication>
#include <Windows.h>
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();

19
mainscreen.cpp Normal file
View File

@ -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;
}

28
mainscreen.h Normal file
View File

@ -0,0 +1,28 @@
//
// Created by HW on 2023/07/27.
//
#ifndef OFFICEASSISTANT_MAINSCREEN_H
#define OFFICEASSISTANT_MAINSCREEN_H
#include <QWidget>
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

22
mainscreen.ui Normal file
View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<author/>
<comment/>
<exportmacro/>
<class>MainScreen</class>
<widget class="QWidget" name="MainScreen">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>MainScreen</string>
</property>
</widget>
<pixmapfunction/>
<connections/>
</ui>

View File

@ -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;
}

View File

@ -28,10 +28,14 @@ MainWindowLayout::MainWindowLayout(QWidget *parent) :
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"));
}*/

View File

@ -9,6 +9,7 @@
#include <QWidget>
#include <QVBoxLayout>
#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<ButtonStruct *> list;
NavBar *navBar;
ButtonStruct *buttons;
MainScreen *mainScreen;
};

View File

@ -12,18 +12,19 @@ NavBar::NavBar(QList<ButtonStruct *> &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<<button;
}
layout->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();
}
}

View File

@ -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<MyButton *> buttons;
int width2;
int height2;
};