QT Creator 4.7.0 + QT 5.9.6 項目問題記錄與解決
問題:對QT中窗口Widget設(shè)置背景圖片border-image導(dǎo)致窗口的所有控件背景圖片都變了征讲。
關(guān)鍵字:背景
思路:在 設(shè)計 界面毁欣,Widget屬性氮块,在 styleSheet 中設(shè)置 border-image 并在限制范圍。
實作:命名Widget,并限制范圍杰捂。
范例:Widget名稱為Status_monitor
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" contenteditable="true" cid="n7" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#Status_monitor{border-image: url(:/res/image/background-.bmp);}</pre>
需求:在一個用Qlabel顯示的圖片上的特定位置顯示文字比驻。不使用布局,控件排列與自適應(yīng)不易认轨;使用布局后绅络,控件如何重疊,
關(guān)鍵字:控件重疊、Widget重疊恩急、GUI控件自適應(yīng)杉畜、控件按比例縮放
思路:方案一:手動排列GUI,用代碼實現(xiàn)控件自適應(yīng)衷恭;方案二:解決如何使用布局并控件重疊此叠。
實作:在Widget中加入子Widget,在子Widget中加入Qlabel并于其中布局完成随珠,再手動修改QT UI FILE拌蜘,將Widget改為Qlabel,最后將其融入整體布局中牙丽。
范例:參考CSDN
需求:在Qlabel顯示的圖片上畫線简卧。
關(guān)鍵字:在label控件上畫線、控件 畫線烤芦、控件 畫圖举娩、在任意控件上畫圖
思路:繼承一個QLabel,然后在paintEvent中畫构罗。
實作:參考CSDN
用向?qū)梢粋€新類比如LineLabel铜涉,繼承于QLabel。
添加虛函數(shù) virtual void paintEvent(QPaintEvent *) override 在其中編寫繪畫內(nèi)容
在 設(shè)計界面 遂唧,找到作為底圖的QLabel芙代,在 對象查看 中 右鍵點擊類名稱,選擇提升盖彭,里面填LineLabel纹烹,保存,重新生成召边。
linelabel.h
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" contenteditable="true" cid="n29" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#ifndef LINELABEL_H
define LINELABEL_H
include <QLabel>
include <QPoint>
include <QColor>
include <QPaintEvent>
?
class LineLabel : public QLabel
{
public:
LineLabel(QWidget *parent = nullptr);
?
//繪制線條
virtual void paintEvent(QPaintEvent *event) override;
//得到畫線的起點和終點
void getStartPoint(QPoint SPoint);
void getEndPoint(QPoint EPoint);
?
private:
QPoint lineStartPoint; //畫線起點
QPoint lineEndPoint; //畫線終點
QColor lineColor; //線條顏色
int lineSize;
};
?
endif // LINELABEL_H</pre>
linelabel.cpp
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" contenteditable="true" cid="n32" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#include "linelabel.h"
include <QPen>
include <QPainter>
?
//初始化
LineLabel::LineLabel(QWidget *parent):QLabel(parent)
{
this->lineStartPoint = QPoint(0,0);
this->lineEndPoint = QPoint(0,0);
this->lineColor = QColor(Qt::red);
this->lineSize = 3;
}
?
//繪制線條
void LineLabel::paintEvent(QPaintEvent *event)
{
QLabel::paintEvent(event);
QPainter painter(this);
QPen pen;
pen.setColor(lineColor);
pen.setWidth(lineSize);
painter.setPen(pen);
painter.drawLine(lineStartPoint,lineEndPoint);
}
?
void getStartPoint(QPoint SPoint)
{
lineStartPoint = SPoint铺呵;
}
?
void getEndPoint(QPoint EPoint)
{
lineEndPoint = EPoint;
}
?</pre>
status_monitor.cpp
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="C++" contenteditable="true" cid="n35" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 1em 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">#include "status_monitor.h"
include "ui_status_monitor.h"
?
Status_monitor::Status_monitor(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Status_monitor)
{
ui->setupUi(this);
QTimer *timer = new QTimer(this);
connect(timer,SIGNAL(timeout()),this,SLOT(timerUpdate()));
timer->start(1000);
}
?
Status_monitor::~Status_monitor()
{
delete ui;
}
?
?
void Status_monitor::timerUpdate(void)
{
int x1, x2, y1, y2;
x1 = 0;
y1 = 0;
x2 = 100;
y2 = 100;
ui->lbTIME->getStartPoint(x1,y1);
ui->lbTIME->getEndPoint(x2,y2);
ui->lbTIME->update();
}</pre>
使用windeployqt打包封裝后遇到數(shù)據(jù)庫連接異常
關(guān)鍵字:打包、封裝隧熙、數(shù)據(jù)庫
思路:缺啥補啥片挂。
實作:把 libmysql.lib 和 libmysql.dll 復(fù)制到exe同級目錄下。
另外打包封裝參考CSDN贞盯。