qt自定義控制元件-多類別提示框
阿新 • • 發佈:2021-02-20
技術標籤:qt MyCustomWidgetqt自定義控制元件qt提示框滑動選擇器qt滑動選擇提示框qt多功能複選框
一、前言
用慣了自帶的messagebox,或者感覺效果不是很好看,或者有介面的特殊美觀需求等等,那麼就定製吧。
1.單選項提示框
2.雙選項提示框
3.滾動選項提示框
4.多組合選項提示框
5.密碼提示框
二、環境
qt57
window10
三、正文
畫布多少,直接說核心部分
1.單選項提示框
背景是哥圖片,可以自定義為任何不規則圖片,然後傳輸想要顯示的文字通過槽函式傳送訊號,新建一個widget用於顯示,開啟之後再初始化中加入定時器自動關閉介面,避免長時間沒操作。
messagebox1.h
protected:
void paintEvent(QPaintEvent *paintevent);
private slots:
void on_pushButton_clicked();
void receivetext(QString res);
private:
Ui::MessageBox1 *ui;
QPixmap pic;
messagebox1.cpp
#include "messagebox1.h" #include "ui_messagebox1.h" MessageBox1::MessageBox1(QWidget *parent) : QDialog(parent), ui(new Ui::MessageBox1) { ui->setupUi(this); this->setWindowModality(Qt::ApplicationModal);//設定一直保持在頂端,不可切換其他介面,除非被新介面帶此屬性覆蓋 this->setWindowFlags(Qt::FramelessWindowHint);//設定窗體無邊框 this->setAttribute(Qt::WA_TranslucentBackground);//設定背景透明 pic.load(tr(":/PIC/background/message1.png"));//設定背景 //倒計時關閉介面 for(int i=0;i<10;i++) QTimer::singleShot(i*1000,this,[=](){ui->pushButton->setText(QString("確認(%1)").arg(9-i));}); QTimer::singleShot(10*1000,this,[=](){this->close();}); ui->pushButton->setFocus();//設定聚焦,作用你懂的 } MessageBox1::~MessageBox1() { delete ui; } void MessageBox1::paintEvent(QPaintEvent *paintevent) { paintevent->ignore(); QPainter painter(this); painter.drawPixmap(0, 0, pic);//繪製圖像 } void MessageBox1::on_pushButton_clicked() { this->close(); } void MessageBox1::receivetext(QString res) { ui->label->setText(res); }
ui中加入button和label即可
主函式呼叫
MessageBox1 * box = new MessageBox1();
box->show();
box->setAttribute(Qt::WA_DeleteOnClose);//若是關閉介面,則徹底釋放資源
connect(this,SIGNAL(sendtext(QString)),box,SLOT(receivetext(QString)));
emit sendtext("提示!\r\n這是一個測試訊息!");
2.雙選項提示框
參考上面的,核心部分就是觀測exec返回值0或1,代表是否點選確認,還是點選取消,還是強制或者自動關閉的提示框介面, 預設確認為1,其餘為0
在初始化里加入this->reject();//exec返回值為0
在確認按鍵點選之後加入this->accept();//exec返回值為1
主函式呼叫
MessageBox2 * box = new MessageBox2();
box->show();
box->setAttribute(Qt::WA_DeleteOnClose);//若是關閉介面,則徹底釋放資源
connect(this,SIGNAL(sendtext(QString)),box,SLOT(receivetext(QString)));
emit sendtext("提示!\r\n這是一個測試訊息!");
qDebug()<<box->exec();
這裡的判斷box->exec()返回值就和正常的messagebox使用判斷一樣了
3.滾動選項提示框
主要就是把第2個的label換成了一個自定義控制元件,自定義控制元件的功能是滾動選擇,然後繫結一些槽配套使用再傳遞給主函式即可
messagebox3.h
#ifndef MESSAGEBOX3_H
#define MESSAGEBOX3_H
#include <QDialog>
#include "common.h"
#include "CustomWidget/Slidingselector/slidingselector.h"
namespace Ui {
class MessageBox3;
}
class MessageBox3 : public QDialog
{
Q_OBJECT
public:
explicit MessageBox3(QWidget *parent = 0);
~MessageBox3();
protected:
void paintEvent(QPaintEvent *paintevent);
signals:
void sendchoose(QString);
private slots:
void on_btn_sure_clicked();
void on_btn_false_clicked();
void receivetext(QStringList res,int num);
private:
Ui::MessageBox3 *ui;
QPixmap pic;
QStringList m_strlist;//所有滾動項列表
SlidingSelector *roll;
QString choose;
};
#endif // MESSAGEBOX1_H
messagebox3.cpp
#include "messagebox3.h"
#include "ui_messagebox3.h"
MessageBox3::MessageBox3(QWidget *parent) :
QDialog(parent),
ui(new Ui::MessageBox3)
{
ui->setupUi(this);
this->setWindowModality(Qt::ApplicationModal);//設定一直保持在頂端,不可切換其他介面,除非被新介面帶此屬性覆蓋
this->setWindowFlags(Qt::FramelessWindowHint);//設定窗體無邊框
this->setAttribute(Qt::WA_TranslucentBackground);//設定背景透明
pic.load(tr(":/PIC/background/message2.png"));//設定背景
roll=new SlidingSelector;
roll->show();
ui->horizontalLayout->addWidget(roll);
roll->setMode_Hor_Ver(Vertical);//設定垂直模式
roll->setMode_Num_Str(String);//設定字串模式
connect(roll,&SlidingSelector::currentStrChanged,[=](QString aaa){
choose=aaa;
//qDebug()<<aaa;
});
//倒計時關閉介面
for(int i=0;i<30;i++)
QTimer::singleShot(i*1000,this,[=](){ui->btn_false->setText(QString("取消(%1)").arg(29-i));});
QTimer::singleShot(30*1000,this,[=](){this->close();});
ui->btn_false->setFocus();//設定聚焦,作用你懂的
}
MessageBox3::~MessageBox3()
{
delete ui;
}
void MessageBox3::paintEvent(QPaintEvent *paintevent)
{
paintevent->ignore();
QPainter painter(this);
painter.drawPixmap(0, 0, pic);//繪製圖像
}
void MessageBox3::on_btn_sure_clicked()
{
emit sendchoose(choose);
this->close();
}
void MessageBox3::on_btn_false_clicked()
{
this->close();
}
void MessageBox3::receivetext(QStringList res,int num)
{
m_strlist=res;//m_strlist備用
roll->setRange(res);
choose=res.at(num);//設定當前值,避免直接不改變直接確認返回值無效
roll->setCurrentStr(choose);
//qDebug()<<res;
}
slidingselector自定義控制元件下載
ui中將label改為horizontalLayout即可
主函式呼叫
MessageBox3 * box = new MessageBox3();
box->show();
box->setAttribute(Qt::WA_DeleteOnClose);//若是關閉介面,則徹底釋放資源
connect(this,SIGNAL(sendtext(QStringList,int)),box,SLOT(receivetext(QStringList,int)));
QStringList list;
list<<"str撒撒發生111"<<"str撒撒發生22"<<"str撒撒發生33"<<"str撒撒發生44"<<"str撒撒發生55"<<"str撒撒發生66";
emit sendtext(list,2);
connect(box,&MessageBox3::sendchoose,[=](QString res){
qDebug()<<res;
});
滾動自定義控制元件簡介,可設定為橫向或者豎著重新整理模式,可設定為數字或者字串重新整理模式,數字模式傳入範圍值即可,字串模式傳入引數QSteingList即可,可以把目前列表中的值傳入讓其顯示。所有部分顏色均可更改
4.多組合選項提示框
5.密碼提示框
四、結語
世上無難事