Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
說明:
=====
1.1 PySide2 :這個QT的親兒子最近(2018年7月)才出生,而且持續(xù)有團(tuán)隊在維護(hù)。
1.2 PyQt5:是在PySide2之前的,Qt收的義子 (Riverbank Computing這個公司開發(fā)的)。
1.3 Qt庫里面有非常強(qiáng)大的圖形界面開發(fā)庫,但是Qt庫是C++語言開發(fā)的,PySide2、PyQt5可以讓我們通過Python語言使用Qt。推薦使用:PySide2。(終于理順關(guān)系了)
1.4 基于Qt 的Python庫
1.4.1 優(yōu)點(diǎn)是控件比較豐富、跨平臺體驗好、文檔完善、用戶多。
1.4.2 缺點(diǎn)是 庫比較大,發(fā)布出來的程序比較大;當(dāng)然是個好東西,總得介紹介紹。
1.5 大家要開發(fā)小工具,界面比較簡單,可以采用appJar,EasyGUI,PySimpleGUI,我都有介紹:
《自稱是Python中GUI的終極工具:appJar》
《EasyGUI是python的一個超級簡單的GUI工具介紹(一)》
《python3的PySimpleGUI庫的介紹、安裝、學(xué)習(xí)》
2 安裝:
======
2.1 環(huán)境:華為筆記本電腦、深度deepin-linux操作系統(tǒng)、python3.8和微軟vscode編輯器。
2.2 安裝:
#安裝 PySide2
pip install pyside2 #官網(wǎng)方法,太慢
#本機(jī)安裝
#sudo pip3.8 install pyside2 #太慢
#推薦國內(nèi)源安裝
#sudo pip3.8 install -i https://mirrors.aliyun.com/pypi/simple pyside2
最新pyside2和pyqt5都是支持python3.8
2.3 官網(wǎng):
https://doc.qt.io/qtforpython/index.html
https://wiki.qt.io/Qt_for_Python
https://forum.qt.io/category/58/qt-for-python
https://pypi.org/project/PySide2/ #最新版
https://wiki.qt.io/PySide2
2.4 竟然沒有g(shù)ithub地址:
https://github.com/PySide #PySide (deprecated),這個地址指已經(jīng)棄用了
3 helloworld:最簡單的一個GUI窗口開始學(xué)習(xí):
====================================
3.1 多種方法的代碼:
#方法一
from PySide2.QtWidgets import QApplication, QLabel
app=QApplication()
label=QLabel("Hello World")
label.show()
app.exec_()
#方法二
from PySide2.QtWidgets import QApplication, QLabel
if __name__=="__main__":
app=QApplication()
label=QLabel("Hello World")
label.show()
app.exec_()
#方法三
from PySide2.QtWidgets import QApplication, QLabel
def main():
app=QApplication()
label=QLabel("Hello World")
label.show()
app.exec_()
if __name__=="__main__":
main()
#方法四
import sys
from PySide2.QtWidgets import QApplication, QLabel
if __name__=="__main__":
app=QApplication(sys.argv) #python的sys.argv的知識點(diǎn)
label=QLabel("Hello World")
label.show()
sys.exit(app.exec_())
3.2 python的sys.argv:(引申)
3.2.1 sys.argv[0]表示代碼本身文件路徑。
3.2.2 sys.argv[ ]其實(shí)就是一個列表,里邊的項為用戶輸入的參數(shù),關(guān)鍵就是要明白這參數(shù)是從程序外部輸入的,而非代碼本身的什么地方,要想看到它的效果就應(yīng)該將程序保存了,從外部來運(yùn)行程序并給出參數(shù)。
3.3 效果圖:
4 美化上述:
=========
4.1 QLabel的文本的字體大小和顏色:
from PySide2.QtWidgets import QApplication, QLabel
app=QApplication()
#文本設(shè)置:字體大小和顏色定義
label=QLabel("<font color=red size=40>Hello World!</font>")
label.show()
app.exec_()
4.2 窗口大小、位置和標(biāo)題名:
#方法一
from PySide2.QtWidgets import QApplication, QWidget
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("中文顯示窗口")
#窗口位置和大小
self.setGeometry(300,300, 500,400)
self.setIcon()
#myApp=QApplication(sys.argv)
app=QApplication() #與上面相同
window=Window()
window.show()
app.exec_()
#方法二:窗口大小、位置和標(biāo)題名,帶有窗口內(nèi)容顯示文本的
#增加控件:QWidget,QFormLayout,QVBoxLayout
from PySide2.QtWidgets import QApplication, QLabel,QWidget,QFormLayout,QVBoxLayout
app=QApplication()
#自定義Windowti=window title=窗口相關(guān)設(shè)置:類(python的基礎(chǔ)知識)
class Windowti(QWidget):
#初始化特性
def __init__(self):
QWidget.__init__(self)
#窗口標(biāo)題名
self.setWindowTitle('你好世界!HelloWorld!') #支持中文
#self.setMinimumWidth(400) #最小窗口寬度
#窗口大小和位置
#x和y是坐標(biāo),左上角坐標(biāo)為0,0
#w和h是寬和高,即窗口大小
#setGeometry(x: int, y: int, w: int, h: int)
self.setGeometry(10,10,500,500)
# Create the QVBoxLayout that lays out the whole form
self.layout=QVBoxLayout()
# Create the form layout that manages the labeled controls
self.form_layout=QFormLayout()
self.greeting=QLabel('', self)
#self.form_layout.addRow('Hello World!', self.greeting)
self.form_layout.addRow('<font color=red size=40>Hello World!</font>', self.greeting)
# Add the form layout to the main VBox layout
self.layout.addLayout(self.form_layout)
# Set the VBox layout as the window's main layout
self.setLayout(self.layout)
def run(self):
# Show the form
self.show()
# Run the qt application
app.exec_()
myfirstapp=Windowti()
myfirstapp.run()
效果圖:
4.3 qml法:
4.3.1 文件夾:1hw下面有2個文件:main.py和mian.qml
4.3.2 main.py代碼:
from PySide2.QtWidgets import QApplication
from PySide2.QtQml import QQmlApplicationEngine
if __name__=="__main__":
app=QApplication()
#注意main.qml文件的目錄和路徑
engine=QQmlApplicationEngine("/home/xgj/Desktop/pyside2/1hw/main.qml")
app.exec_()
4.3.3 main.qml代碼:注意:安裝插件:Qt for python,這樣qml代碼才有高亮。
import QtQuick 2.7
import QtQuick.Window 2.3
import QtQuick.Controls 2.3
ApplicationWindow {
id: _window
// 窗口標(biāo)題設(shè)置
title: "qml顯示窗口Helloworld!"
// 窗口大小和位置的設(shè)置
width: 800
height: 500
x:300
y:300
// Window默認(rèn)不可見,需要進(jìn)行設(shè)置為可見
visible: true
}
4.3.4 運(yùn)行效果圖:
5 圖片顯示:
========
5.1 靜態(tài)圖片顯示:
from PySide2.QtWidgets import QApplication, QLabel
from PySide2.QtGui import QPixmap
#app=QApplication([])
app=QApplication() #等同于上面
Label=QLabel()
#指定圖片的目錄和路徑,圖片格式:jpeg,jpg,png,ico均可
PixMap=QPixmap("/home/xgj/Desktop/pyside2/20.jpeg")
#PixMap=QPixmap("/home/xgj/Desktop/pyside2/18.jpg")
#PixMap=QPixmap("/home/xgj/Desktop/pyside2/1231.png")
#PixMap=QPixmap("/home/xgj/Desktop/pyside2/1.ico")
#PixMap=QPixmap("/home/xgj/Desktop/pyside2/yytd.gif") #顯示靜態(tài)的,不是gif動態(tài)的
Label.setPixmap(PixMap)
Label.show()
app.exec_()
效果圖:
5.2 gif圖顯示:
from PySide2.QtWidgets import QApplication, QLabel
from PySide2.QtGui import QMovie
#app=QApplication([])
app=QApplication()
Label=QLabel()
#注意不要把這行放到實(shí)例化app對象的上面,會無效
#gif因為是動態(tài)的,屬于movie動畫類
Movie=QMovie("/home/xgj/Desktop/pyside2/yytd.gif") #可以
#Movie=QMovie("/home/xgj/Desktop/pyside2/test.mp4") #報錯
Label.setMovie(Movie)
#這句表示播放動畫,不能省略。也不能放到show后面,否則無法自適應(yīng)大小
Movie.start()
Label.show()
app.exec_()
效果圖:
====很仔細(xì),初始pyside2===
整理并分享出來,喜歡的就點(diǎn)贊、關(guān)注、評論、轉(zhuǎn)發(fā)和收藏。
通用移動類,目標(biāo)就是為了實(shí)現(xiàn)放入任意的控件以后,支持鼠標(biāo)拖動,在容器中或者父類中拖動,這個應(yīng)用場景非常多,比如在地圖上放置的設(shè)備,需要用戶自行按下拖動到指定的合適的位置,然后保存設(shè)備的位置坐標(biāo)到數(shù)據(jù)庫,下次打開直接加載,在一些安防項目、電力項目、環(huán)境監(jiān)測等上面大量運(yùn)用,有時候設(shè)備對應(yīng)了多種類型,以前做的辦法是將這個移動的代碼直接封裝在對應(yīng)設(shè)備的自定義控件中,有個巨大缺點(diǎn)就是如果再新增加一個控件,又需要重復(fù)的代碼加到控件中才行,可否將這個功能獨(dú)立出來,只要傳入控件就行呢,當(dāng)然可以,比如我寫過很多自定義控件,現(xiàn)在需要控件放到某個容器中能自由拖動,只需要new出通用移動類來就行。
項目開源地址:https://gitee.com/feiyangqingyun/QWidgetDemo
#ifndef MOVEWIDGET_H
#define MOVEWIDGET_H
/**
* 通用控件移動類 作者:feiyangqingyun(QQ:517216493) 2019-9-28
* 1:可以指定需要移動的widget
* 2:可設(shè)置是否限定鼠標(biāo)左鍵拖動
* 3:支持任意widget控件
*/
#include <QWidget>
#ifdef quc
#if (QT_VERSION < QT_VERSION_CHECK(5,7,0))
#include <QtDesigner/QDesignerExportWidget>
#else
#include <QtUiPlugin/QDesignerExportWidget>
#endif
class QDESIGNER_WIDGET_EXPORT MoveWidget : public QObject
#else
class MoveWidget : public QObject
#endif
{
Q_OBJECT
public:
explicit MoveWidget(QObject *parent=0);
protected:
bool eventFilter(QObject *watched, QEvent *event);
private:
QPoint lastPoint; //最后按下的坐標(biāo)
bool pressed; //鼠標(biāo)是否按下
bool leftButton; //限定鼠標(biāo)左鍵
QWidget *widget; //移動的控件
public Q_SLOTS:
//設(shè)置是否限定鼠標(biāo)左鍵
void setLeftButton(bool leftButton);
//設(shè)置要移動的控件
void setWidget(QWidget *widget);
};
#endif // MOVEWIDGET_H
【領(lǐng)QT開發(fā)教程學(xué)習(xí)資料,點(diǎn)擊下方鏈接莬費(fèi)領(lǐng)取↓↓,先碼住不迷路~】
點(diǎn)擊這里:「鏈接」
#include "movewidget.h"
#include "qevent.h"
#include "qdebug.h"
MoveWidget::MoveWidget(QObject *parent) : QObject(parent)
{
lastPoint=QPoint(0, 0);
pressed=false;
leftButton=true;
widget=0;
}
bool MoveWidget::eventFilter(QObject *watched, QEvent *event)
{
if (widget !=0 && watched==widget) {
QMouseEvent *mouseEvent=(QMouseEvent *)event;
if (mouseEvent->type()==QEvent::MouseButtonPress) {
//如果限定了只能鼠標(biāo)左鍵拖動則判斷當(dāng)前是否是鼠標(biāo)左鍵
if (leftButton && mouseEvent->button() !=Qt::LeftButton) {
return false;
}
//判斷控件的區(qū)域是否包含了當(dāng)前鼠標(biāo)的坐標(biāo)
if (widget->rect().contains(mouseEvent->pos())) {
lastPoint=mouseEvent->pos();
pressed=true;
}
} else if (mouseEvent->type()==QEvent::MouseMove && pressed) {
//計算坐標(biāo)偏移值,調(diào)用move函數(shù)移動過去
int offsetX=mouseEvent->pos().x() - lastPoint.x();
int offsetY=mouseEvent->pos().y() - lastPoint.y();
widget->move(widget->x() + offsetX, widget->y() + offsetY);
} else if (mouseEvent->type()==QEvent::MouseButtonRelease && pressed) {
pressed=false;
}
}
return QObject::eventFilter(watched, event);
}
void MoveWidget::setWidget(QWidget *widget)
{
if (this->widget==0) {
this->widget=widget;
this->widget->installEventFilter(this);
}
}
void MoveWidget::setLeftButton(bool leftButton)
{
this->leftButton=leftButton;
}
原文鏈接:https://www.cnblogs.com/feiyangqingyun/p/11608377.html
【領(lǐng)QT開發(fā)教程學(xué)習(xí)資料,點(diǎn)擊下方鏈接莬費(fèi)領(lǐng)取↓↓,先碼住不迷路~】
點(diǎn)擊這里:Qt資料領(lǐng)?。ㄒ曨l教程+文檔+代碼+項目實(shí)戰(zhàn))
百度在線地圖的應(yīng)用老早就做過,后面經(jīng)過不斷的完善才到今天的這個程序,除了基本的可以載入地圖并設(shè)置一些相關(guān)的屬性以外,還增加了各種js函數(shù)直接異步加載數(shù)據(jù)比如動態(tài)添加點(diǎn)、矩形、圓形、行政區(qū)劃等各種。當(dāng)然最大的是增加了離線地圖的支持,當(dāng)年這個離線地圖拖了很久很久才去做,最終還是搞定了。
在線地圖沒有太多的難點(diǎn),搞一個簡單的在線地圖demo絕對是分分鐘幾行代碼的事情,在使用過程中就是改進(jìn)了幾個小的地方,比如地圖的邊距,需要設(shè)置增加一行css為 html,body{height:100%;width:100%;margin:0px;padding:0px;},比如左下角有個百度的logo,要去除的話也是增加一行css為 .anchorBL{display:none;},不然發(fā)布出去別人看到了怎么還有百度的logo怪沒有面子的。再比如滾動條的樣式,很多人說我明明設(shè)置了Qt的滾動條樣式啊,為什么這里邊的滾動條沒有效果呢?其實(shí)這里面的滾動條是網(wǎng)頁的,并不受控制的,你需要設(shè)置網(wǎng)頁的滾動條css如下。
::-webkit-scrollbar{width:0.8em;}
::-webkit-scrollbar-track{background:rgb(241,241,241);}
::-webkit-scrollbar-thumb{background:rgb(188,188,188);}
1. 同時支持在線地圖和離線地圖兩種模式。
2. 同時支持webkit內(nèi)核、webengine內(nèi)核、IE內(nèi)核。
3. 支持設(shè)置多個標(biāo)注點(diǎn),信息包括名稱、地址、經(jīng)緯度。
4. 可設(shè)置地圖是否可單擊、拖動、鼠標(biāo)滾輪縮放。
5. 可設(shè)置協(xié)議版本、秘鑰、主題樣式、中心坐標(biāo)、中心城市、地理編碼位置等。
6. 可設(shè)置地圖縮放比例和級別,縮略圖、比例尺、路況信息等控件的可見。
7. 支持地圖交互,比如鼠標(biāo)按下獲取對應(yīng)位置的經(jīng)緯度。
8. 支持查詢路線,可設(shè)置起點(diǎn)位置、終點(diǎn)位置、路線模式、路線方式、路線方案(最少時間、最少換乘、最少步行、不乘地鐵、最短距離、避開高速)。
9. 可顯示點(diǎn)線面工具,可直接在地圖上劃線、點(diǎn)、矩形、圓形等。
10. 可設(shè)置行政區(qū)劃,指定某個城市區(qū)域繪制圖層,在線地圖自動輸出行政區(qū)劃邊界點(diǎn)集合到j(luò)s文件給離線地圖使用。
11. 可靜態(tài)或者動態(tài)添加多個覆蓋物。支持點(diǎn)、折線、多邊形、矩形、圓形、弧線、點(diǎn)聚合等。
12. 函數(shù)接口友好和統(tǒng)一,使用簡單方便,就一個類。
13. 支持js動態(tài)交互添加點(diǎn)、刪除點(diǎn)、清空點(diǎn)、重置點(diǎn),不需要刷新頁面。
14. 支持任意Qt版本、任意系統(tǒng)、任意編譯器。
國內(nèi)站點(diǎn):[https://gitee.com/feiyangqingyun](https://gitee.com/feiyangqingyun)
國際站點(diǎn):[https://github.com/feiyangqingyun](https://github.com/feiyangqingyun)
*請認(rèn)真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。