1. 抓取當前屏幕
QPixmap m_loadPixmap = QPixmap::grabWindow(QApplication::desktop()->winId());
2. qt界面設置邊框-樣式表實現
ui->frame->setStyleSheet("#frame{border:2px solid #014F84}#background-color:#00d8ff");
// 2px solid color 這個順序不能錯,就是這麼坑人;
3. QT QLabel設置字體和顏色
//設置字號
QFont ft;
ft.setPointSize(12);
ui.label_4->setFont(ft);
//設置顏色
QPalette pa;
pa.setColor(QPalette::WindowText,Qt::red);
ui.label_4->setPalette(pa);
4. 窗口整體透明,但窗口內的控件不透明
// QT5 必須放在 ui->setupUi(this) 前;
setAttribute(Qt::WA_TranslucentBackground, true);
ui->setupUi(this);
5. 樣式表
5.1 QSpinBox,QTimeEdit
/*spinbox 擡起樣式*/
QTimeEdit::up-button,QDoubleSpinBox::up-button,QSpinBox::up-button
{
subcontrol-origin:border;
subcontrol-position:right;
image: url(:/ico/up_right.png);
width: 12px;
height: 20px;
}
QTimeEdit::down-button,QDoubleSpinBox::down-button,QSpinBox::down-button
{
subcontrol-origin:border;
subcontrol-position:left;
border-image: url(:/ico/up_left.png);
width: 12px;
height: 20px;
}
/*按鈕按下樣式*/
QTimeEdit::up-button:pressed,QDoubleSpinBox::up-button:pressed,QSpinBox::up-button:pressed
{
subcontrol-origin:border;
subcontrol-position:right;
image: url(:/ico/pushed_right.png);
width: 12px;
height: 20px;
}
QTimeEdit::down-button:pressed,QDoubleSpinBox::down-button:pressed, \
QSpinBox::down-button:pressed,QSpinBox::down-button:pressed
{
subcontrol-position:left;
image: url(:/ico/pushed_left.png);
width: 12px;
height: 20px;
}
5.2 QDialog設置圓角
// 1. QDialog 構造函數中,其中 juxing.png 爲圓角圖片;
QPixmap pm(":/subject/Resources/YingYu/juxing.png");
setMask(pm.mask());
// 2. 樣式表中 寫
QDialog#Dialog_ENYinBiao
{
... ...
border-radius:1em;
}
5.3 QSlider 滑塊
// 水平 用 horizontal
// 垂直 用 vertical
// 以水平爲例,如下:
//首先是設置主體
QSlider#horizontalSlider_Pen{
width: 493;
height: 40;
border:0px;
opacity: 0.7;
}
/* 滑塊 QSlider - handle */
QSlider#horizontalSlider_Pen::handle:horizontal{
width: 30;
height: 30;
border:0px;
opacity: 0.7;
margin-left:30px;
margin-right:40px;
background-image: url(:/PenDialog/Resources/slider_handle.png);
}
/* 滑槽 QSlider - groove */
QSlider#horizontalSlider_Pen::groove:horizontal{
width: 575;
height: 34;
border:0px;
opacity: 0.7;
background-image: url(:/PenDialog/Resources/slider_groove.png);
}
//還沒有滑上去的地方
QSlider::add-page:horizontal
{
background: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 #bcbcbc, stop:0.25 #bcbcbc, stop:0.5 #bcbcbc, stop:1 #bcbcbc);
}
//已經劃過的從地方
QSlider::sub-page:horizontal
{
background: qlineargradient(spread:pad, x1:0, y1:1, x2:0, y2:0, stop:0 #439cf3, stop:0.25 #439cf3, stop:0.5 #439cf3, stop:1 #439cf3);
}
5.4 QRadioButton
QRadioButton {
spacing: 5px;
font-size: 14px;
color: rgb(24, 220, 88); # 連同 font-size一齊設置按鈕的文本樣式
}
QRadioButton::indicator { # indicator是一個子組件,這裏的width和height分別指定按鈕的寬和高
width: 13px;
height: 13px;
}
QRadioButton::indicator::unchecked { # 未選中時狀態,也即正常狀態
image: url(:/images/radiobutton_unchecked.png);
}
QRadioButton::indicator:unchecked:hover { # 未選中時,鼠標懸停時的狀態
image: url(:/images/radiobutton_unchecked_hover.png);
}
QRadioButton::indicator:unchecked:pressed { #未選中時,按鈕下按時的狀態
image: url(:/images/radiobutton_unchecked_pressed.png);
}
QRadioButton::indicator::checked { # 按鈕選中時的狀態
image: url(:/images/radiobutton_checked.png);
}
QRadioButton::indicator:checked:hover { # 按鈕選中時,鼠標懸停狀態
image: url(:/images/radiobutton_checked_hover.png);
}
QRadioButton::indicator:checked:pressed { # 按鈕選中時,鼠標下按時的狀態
image: url(:/images/radiobutton_checked_pressed.png);
}
QRadioButton::indicator:disabled { # 按鈕禁用時的狀態
image: url(:/buttonbg/radio_disable);
}
5.5 設置 QSS 的動態屬性 -- setPropery
// 在代碼中
iconLabel->setProperty("labelState", "hover");
textLabel->setProperty("labelState", "hover");
// 在 QSS 文件中
QLabel#objectname[labelState="hover"]
{
image: url(:/Resouces/images/refresh.png);
}
/*鼠標劃過按鈕顯示的文字樣式*/
QLabel#objectname[labelState="hover"]
{
color:rgb(0 ,255 , 252);
}
6. 半透明 遮罩
//添加黑色遮罩
QWidget *mask = new QWidget;
mask->setAutoFillBackground(true);
mask->setPalette(QPalette(QColor(0, 0, 0, 50)));//alpha爲不透明度
mask->resize(900, 600);
7.程序啓動畫面
QApplication app(argc, argv);
//QTime提供了鬧鐘功能
qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));//secsTo()爲返回當前的秒數
QPixmap pix(":/images/logo.png");
QSplashScreen splash(pix);
splash.resize(pix.size());
splash.show();
app.processEvents();//調用該函數的目的是爲了使程序在啓動畫面的同時仍然能夠響應鼠標事件
... ...
splash.finish(&view);//當窗口view完成初始化工作後就結束啓動畫面
app.exec();
8. 圖片旋轉
QT 圖片旋轉函數 QPainter::rotate() -- 以圖片中心爲旋轉點,不能隨意設置旋轉點
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
QPoint center(this->rect().center());
painter.translate(center); //讓圖片的中心作爲旋轉的中心
painter.rotate(-m_angle); //順時針旋轉 -m_angle 度
painter.translate(-center); //使原點復原
9. QT對數字自動補0
QString str = QString("%1").arg(12, 5, 10, QLatin1Char('0'));
// 結果: 00012
// 說明: 將數字 12 轉成 10 進制的 5 位字符串,在前面自動補 0
10. 鼠標懸停在某窗口上,改變鼠標圖片
QPushButton * m_btnMove = new QPushButton();
m_btnMove->setCursor(QCursor(Qt::SizeAllCursor));
// setCursor() 表示鼠標在窗體 m_btnMove 上時,改變鼠標圖片