20210411(qt)
vs换qt
问题
1、关于界面文件.ui构建的头文件的include位置
在前者中,界面文件构建的头文件(比如:addressbook.ui对应的ui_addressbook.h),在相应的addressbook.cpp中包含(#include “ui_addressbook.h”)。而后者则是在addressbook.h文件中(#include “ui_addressbook.h”)。所以有时候在前者的addressbook.h文件中使用一些类的时候,需要注意一下是否已经将该类所在的头文件include进来了。
————————————————
版权声明:本文为CSDN博主「morning_sun_lee」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq295456059/article/details/52781726
2、关于定义界面类实例(*ui或者ui)的形式
qt前者默认定义界面类的指针*ui:
vs后者默认定义界面类的变量ui:
所以在调用ui的成员函数以及成员变量的时候,前者只能使用“->”,而后者可以使用“.”和“->”。
3、使用数据库以及xml类
在我们的项目中,使用到了数据库相关以及和xml文件操作相关的类。
对于前者,需要在.pro文件中添加QT+=sql和QT+=xml,并include相应的头文件即可。
而对于后者,不需要在.pro文件中添加语句,但是,在新建项目的时候应该勾选相应的选项如下图:
新建之后,右键项目属性,查看附属依赖项,就会看到添加了相应的.lib文件:
这里需要强调另外的一个问题,这里的.lib都是以d结尾的,也就是说,都是用于Debug模式下的,如果要使用Release模式下的,还需要添加去掉d的.lib(添加的方法很简单,直接在已有的.lib下面输入
4、使用OpenCV
要想使用OpenCV,当然最最首先是需要下载OpenCV库。
在前者,使用OpenCV,需要在.pro文件中添加如下语句:
INCLUDEPATH+=D:\Work_Software\OpenCV3.1\opencv\build\include
LIBS+=D:\Work_Software\OpenCV3.1\opencv\build\x64\vc12\lib\*.lib
5、关于当前路径
Mat img = imread("me.jpg");
想要读入一张名称为me的,jpg文件,那么应该将文件放在什么位置呢?
对于前者来说,
如果是在Debug模式下,则默认的当前目录是与工程文件在同一文件夹下的类似于“build-Test_Project-Desktop_Qt_5_7_0_MSVC2013_64bit-Debug”命名格式的文件。
如果是在Release模式下,则默认的当前目录是与工程文件在同一文件夹下的类似于“build-Test_Project-Desktop_Qt_5_7_0_MSVC2013_64bit-Release”命名格式的文件。
也就是说,需要将me.jpg放在相应的文件夹下面,不然是读不进来的。
而对于后者来说,
当前目录指的是项目文件夹。
————————————————
版权声明:本文为CSDN博主「morning_sun_lee」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq295456059/article/details/52781726
还有,之前一直很好奇#include <QLabel>和#include “qlabel.h”这两种形式有什么区别,后来直接打开QLabel文件,发现里面有且仅有一句代码:没错,就是:#include “qlabel.h” 。。。 。。。
小试牛刀
QCoolPage
界面初始化(控制样式)
初始化界面MainWindow
#ifndef MAINOBJECT_H
#define MAINOBJECT_H
#include <QObject>
class MainWindow;
class MainObject : public QObject
{
Q_OBJECT
public:
explicit MainObject(QObject *parent = 0);
~MainObject();
public :
void setInit();//初始化
private:
MainWindow *m_mainWindow;
};
#endif // MAINOBJECT_H
创建闪屏
#ifndef SPLASHSCREEN_H
#define SPLASHSCREEN_H
#include <QSplashScreen>
#include <QPropertyAnimation>
class SplashScreen : public QSplashScreen
{
Q_OBJECT
public:
explicit SplashScreen(const QPixmap &pixmap);
~SplashScreen();
static SplashScreen *getInstance();
protected:
void paintEvent (QPaintEvent *event);
public:
void setStagePercent(const int &percent,const QString &message);
void setStart(QWidget *widget, const QString &title,const QString &logoFile);
void setFinish();
private:
double m_percent;
QWidget *m_mainWidget;
QPixmap m_pixLogo;
QString m_textLogo;
QString m_message;
static SplashScreen *m_instance;
QPropertyAnimation *m_propertyAnimation;
private:
const int ANIMATION_DURATION = 1000;
};
#endif // SPLASHSCREEN_H
void MainObject::setInit()
{
SplashScreen::getInstance()->setStagePercent(0,tr("初始化应用程序 ..."));
m_mainWindow = new MainWindow(nullptr);
m_mainWindow->setWindowTitle(tr("QCoolPage"));
SplashScreen::getInstance()->setStart(m_mainWindow, tr("QCoolPage"), QString(":/res/res/image/image.png"));
SplashScreen::getInstance()->setStagePercent(40, tr("初始化主界面 ..."));
SplashScreen::getInstance()->setStagePercent(80, tr("加载界面 ..."));
SplashScreen::getInstance()->setStagePercent(100, tr("加载完毕!"));
SplashScreen::getInstance()->setFinish();
m_mainWindow->showNormal();
}
流程
开始
// 开始
void SplashScreen::setStart(QWidget *widget, const QString &title, const QString &logoFile)
{
if (nullptr != widget)
{
m_mainWidget = widget;
m_pixLogo = QPixmap(logoFile);//画logo像素
m_textLogo = title;//动作描述
m_mainWidget->setWindowOpacity(0.0);//设置窗体透明度
if (nullptr == m_propertyAnimation)
{
//控制属性窗体从透明到不透明
m_propertyAnimation = new QPropertyAnimation(m_mainWidget, "windowOpacity");
m_propertyAnimation->setDuration(ANIMATION_DURATION);
m_propertyAnimation->setStartValue(0.0);
m_propertyAnimation->setEndValue(1.0);
}
}
}
设置进度
// 设置进度
void SplashScreen::setStagePercent(const int &percent, const QString &message)
{
if(this->isHidden())
{
this->show();
}
if(!this->isActiveWindow())
{
this->activateWindow();
this->raise();
}
m_message = message;
while (m_percent < percent)
{
m_percent = m_percent + 0.02;
qApp->processEvents();
this->repaint();
}
}
结束
// 结束
void SplashScreen::setFinish()
{
this->close();
if (nullptr != m_mainWidget)
{
m_mainWidget->activateWindow();
m_mainWidget->raise();
}
if (nullptr != m_propertyAnimation)
{
//控制主界面的透明度缓慢提升
m_propertyAnimation->start();
}
}
中间有个绘制事件
进度条
// 绘制事件
void SplashScreen::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);//没有实质性的作用,用来避免编译器警告
const int OFFSET_VALUE = 70;
const int SLIDER_HEIGHT = 10;
const int BORDER_X_RADIUS = 8;
const int BORDER_Y_RADIUS = 4;
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);//反走样
QFont font(QString("微软雅黑"));
QPen painterPen;
painterPen.setColor(Qt::white);
painter.setPen(painterPen);
QBrush brush(QColor(100, 100, 100 ,255));
painter.setBrush(brush);
painter.drawRoundedRect(OFFSET_VALUE, this->height() - OFFSET_VALUE
,(this->width()- OFFSET_VALUE * 2)
,BORDER_X_RADIUS,BORDER_Y_RADIUS,SLIDER_HEIGHT);
// 绘制消息文本
{
font.setPixelSize(16);//设置字体高度占用的像素大小
font.setBold(true);
painter.setFont(font);
painterPen.setColor(Qt::yellow);
painter.setPen(painterPen);
painter.drawText(0, this->height()- OFFSET_VALUE * 2, this->width(), OFFSET_VALUE, Qt::AlignCenter, m_message);
}
// 绘制进度条
{
painterPen.setColor(Qt::white);
painter.setPen(painterPen);
if(m_percent != 0)
{
QBrush brush(QColor(150, 230, 100 ,255));
painter.setBrush(brush);
painter.drawRoundedRect(OFFSET_VALUE, this->height() - OFFSET_VALUE
, (this->rect().width()- OFFSET_VALUE * 2) * m_percent / 100
, BORDER_X_RADIUS,BORDER_Y_RADIUS,SLIDER_HEIGHT);
}
}
// 绘制进度文本
{
font.setPixelSize(14);
font.setBold(false);
painter.setFont(font);
painterPen.setColor(Qt::white);
painter.setPen(painterPen);
QString drawText = QString::number(m_percent,'f',0) + QString("%");
painter.drawText(0, this->height() - OFFSET_VALUE, this->width(), OFFSET_VALUE, Qt::AlignCenter, drawText);
painter.end();
}
// 绘制圆角窗体
{
QPainterPath painterPathPath;
QRectF rect = QRectF(0, 0, this->width(), this->height());
painterPathPath.addRoundRect(rect, BORDER_X_RADIUS, BORDER_X_RADIUS);
QPolygon polygon= painterPathPath.toFillPolygon().toPolygon();
QRegion region(polygon);
this->setMask(region);
}
}
控制样式表
主窗口
// 初始化
void MainWindow::initValue()
{
// 创建系统托盘
syetemTray = new SystemTray(this);
connect(syetemTray->getShowWidget(), SIGNAL(triggered(bool)), this, SLOT(showNormal()));
connect(syetemTray->getMinWidget(), SIGNAL(triggered(bool)), this, SLOT(showMinimized()));
ui->widgetTitle->setParentWidget(this);
this->setWindowFlags(Qt::FramelessWindowHint);
// 将TreeWidget点击的index与TabWidget页面显示的index相互绑定
connect(ui->widgetTree, SIGNAL(sendShowIndex(WidgetTabType)), ui->widgetTab, SLOT(receiveShowCurrentTab(WidgetTabType)));
}
宏函数
#ifndef LOADQSS
#define LOADQSS(qssFile) \
{ \
QFile file(qssFile); \
file.open(QFile::ReadOnly); \
if(file.isOpen()) \
{ \
qApp->setStyleSheet(file.readAll()); \
file.close(); \
} \
}
#endif
/********************** MainWindow Style Sheet *******************/
QMainWindow
{
background-color:#1A1A1A;
color:white;
border:none;
}
QFrame#frameDot
{
border:none;
}
QFrame#frameBackground
{
background-color: #1A1A1A;
border:none;
}
QFrame#frameBorder,#frameBackground
{
background-color:#1F1F1F;
color:white;
border:none;
}
QLabel
{
color: #FFFFFF;
}
/********************** MainTitleBar Style Sheet *******************/
QPushButton#pushButtonClose
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/close_normal.png);
}
QPushButton#pushButtonClose:hover
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/close_hover.png);
}
QPushButton#pushButtonMin
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/min_normal.png);
}
QPushButton#pushButtonMin:hover
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/min_hover.png);
}
QPushButton#pushButtonMax
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/max_normal.png);
}
QPushButton#pushButtonMax:hover
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/max_hover.png);
}
QPushButton#pushButtonNormal
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/normal_normal.png);
}
QPushButton#pushButtonNormal:hover
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/normal_hover.png);
}
QPushButton#pushButtonSave:hover,#pushButtonAdd:hover
{
background-color: rgb(180, 85, 100);
color:white;
}
QPushButton#pushButtonSave,#pushButtonAdd
{
background-color:rgb(84, 115, 135);
color:white;
font-size:12px;
width: 150px;
border-radius:2px;
spacing:2px;
padding: 0px 0px;
}
QScrollBar:vertical
{
background:#1A1A1A;
padding-top:20px;
padding-bottom:20px;
padding-left:3px;
padding-right:3px;
border-left:1px solid #1A1A1A;;
}
QScrollBar::handle:vertical
{
background:#3F3F3F;
border-radius:6px;
min-height:80px;
border-radius:4px;
}
QScrollBar::handle:vertical:hover
{
background:#3F3F3F;
border-radius:4px;
}
QScrollBar::add-page
{
background:#1A1A1A;
}
QScrollBar::sub-page
{
background:#1A1A1A;
}
QScrollBar::add-line:vertical
{
background:url(none) center no-repeat;
}
QScrollBar::sub-line:vertical
{
background:url(none) center no-repeat;
}
/********************** ControlFoldWidget Style Sheet *******************/
QTreeWidget, QTabWidget > QWidget
{
background:transparent;
color:white;
border:none;
}
QTreeWidget::item:selected
{
background-color: #123456;
color:white;
}
QTreeWidget::item
{
height:50px;
}
QTreeWidget::item:!selected:hover
{
background:transparent;
}
QTreeWidget::branch:selected
{
background-color: #123456;
}
/********************** ControlTabWidget Style Sheet *******************/
QTabBar::tab
{
min-width:100px;
color: white;
background-color:#1F1F1F;
border: 0px solid;
padding:6px;
}
QTabBar::tab:!selected
{
margin-top: 5px;
}
QTabBar::tab:selected
{
background-color:#2F2F2F;
color: white;
font-size:14px;
font-weight:bold;
border-bottom: 1px solid;
border-bottom-color:darkred;
}
QTabWidget::pane
{
background-color:transparent;
}
*{
outline:0px;
}
/********************** FrameControl Style Sheet *******************/
QFrame#frameApplication
{
border:1px solid #BBBBBB;
border-radius:5px;
}
QFrame#frameApplication:hover
{
border:1px solid #FFFFFF;
border-radius:5px;
}
QWidget#scrollAreaWidgetContents
{
background-color: #1A1A1A;
border:none;
}
/********************** TableWidget Style Sheet *******************/
QLabel#labelTableText
{
font-family: Microsoft YaHei;
font-size: 18px;
color: #FFFFFF;
}
QHeaderView
{
background-color:transparent;
}
QHeaderView::section
{
border: none;
background-color: #2B2B2B;
font-family: Microsoft YaHei;
font-size: 12px;
color: #FFFFFF;
text-align: center;
min-height: 36px;
max-height: 36px;
}
QTableWidget
{
border:0px solid;
border-top-width:1px;
border-top-style:solid;
border-top-color:#5C8290;
background-color:transparent;
alternate-background-color: #2A2A2A;
}
QTableWidget::item
{
font-family: Microsoft YaHei;
font-size: 10px;
min-height: 36px;
max-height: 36px;
}
QTableWidget::item:selected
{
out-line:none;
}
/********************** NoveControl Style Sheet *******************/
QPushButton#pushButtonCloseControl
{
border:none;
background:rgba(255, 255, 255, 0);
image: url(:/res/res/image/other/close.png);
}
/********************** SliderWidget Style Sheet *******************/
QFrame#frameSlider
{
border:none;
background-color:#1A1A1A;
}
QPushButton#pushButtonLeft
{
border-image: url(:/res/res/image/left_arrow_normal.png);
}
QPushButton#pushButtonLeft:hover
{
border-image: url(:/res/res/image/left_arrow_hover.png);
}
QPushButton#pushButtonLeft:pressed
{
border-image: url(:/res/res/image/left_arrow_pressed.png);
}
QPushButton#pushButtonRight
{
border-image: url(:/res/res/image/right_arrow_normal.png);
}
QPushButton#pushButtonRight:hover
{
border-image: url(:/res/res/image/right_arrow_hover.png);
}
QPushButton#pushButtonRight:pressed
{
border-image: url(:/res/res/image/right_arrow_pressed.png);
}
/********************** SystemTray Style Sheet *******************/
QMenu
{
background-color: white;
border: 1px solid white;
}
QMenu::item
{
background-color: transparent;
padding:8px 32px;
margin:0px 0px;
border-bottom:1px solid #DBDBDB;
}
QMenu::item:selected
{
background-color: #2dabf9;
}
/********************** ProcessControl Style Sheet *******************/
QFrame#frameProcess
{
background:transparent;
border:1px solid rgb(255, 220, 220);
border-radius: 4px;
}
/********************** ListControl Style Sheet *******************/
QFrame#frameLine
{
background: rgba(255, 220, 220, 40);
}
QLabel#labelAppName
{
font-family: Microsoft YaHei;
font-size: 16px;
color: #FFFFFF;
}
QLabel#labelAppText
{
font-family: Microsoft YaHei;
font-size: 12px;
color: #AAAAAA;
}