一般用Qt进行软件开发时,经常使用信号+槽的方式进行函数动作相应。方法一般有两种:
- 利用designer设置信号/槽函数后,源文件在slots里面直接写函数实现
- 直接使用程序connect函数,在源码里面添加关联
上述两种方法,对于一般的动作相应都能实现。但对于不同窗口之间的消息传递可能会有问题!例如,有窗口1和窗口2,点击窗口1中某按钮,窗口2中进行某函数动作。对于这种情况,我们常采用emit关键字来实现。
下面用自定义的窗体展示一下:
(1)、自定义的PNG按钮Widget:
头文件
#ifndef YTEP_CTRL_PNGBUTTON_H
#define YTEP_CTRL_PNGBUTTON_H
#define YTEP_PNG_WIDGET_BORDER 8
#define YTEP_PNG_WIDGET_LABEL 30
#define YTEP_PNG_WIDGET_SEP 10
#include <QWidget>
#include <QPushButton>
#include <QLabel>
class YTEP_CTRL_PngButton : public QWidget
{
Q_OBJECT
public:
explicit YTEP_CTRL_PngButton(QWidget *parent = nullptr);
private:
QPushButton* m_pPushButton;
QLabel* m_pLabel;
QString m_szBtnCaption;
QString m_szIconPath;
QSize m_sizeIcon;
QSize m_sizeWidget;
QRect m_rectIcon;
QRect m_rectLabel;
public:
void SetCaption(QString str);
void SetIconPath(QString path);
QString GetCaption();
QString GetIconPath();
QSize CalcLayout();
void LayoutControls();
void SetCaptionColor(QRgb rc);
QSize GetSize(){return m_sizeWidget;};
public slots:
void push_clicked();
void label_clicked();
signals:
void clicked();
};
#endif // YTEP_CTRL_PNGBUTTON_H
实现文件:
#include "ytep_ctrl_pngbutton.h"
#include <QPushButton>
#include <QLabel>
#include <QPixmap>
YTEP_CTRL_PngButton::YTEP_CTRL_PngButton(QWidget *parent) : QWidget(parent)
{
m_pPushButton = new QPushButton(this);
m_pLabel = new QLabel(this);
connect(m_pPushButton,SIGNAL(clicked()),this,SLOT(push_clicked()));
connect(m_pLabel,SIGNAL(clicked()),this,SLOT(label_clicked()));
}
void YTEP_CTRL_PngButton::SetCaption(QString str)
{
m_szBtnCaption = str;
if (m_pLabel != NULL)
{
m_pLabel->setText(str);
m_pLabel->setStyleSheet("color:rgb(35,43,131);");
QFont m_nFont("宋体",14,900,0);
m_pLabel->setFont(m_nFont);
m_pLabel->setAlignment(Qt::AlignCenter | Qt::AlignHCenter);
//m_pLabel->setFrameStyle(QFrame::Box|QFrame::Raised);
}
}
void YTEP_CTRL_PngButton::push_clicked()
{
emit clicked();
}
void YTEP_CTRL_PngButton::label_clicked()
{
emit clicked();
}
void YTEP_CTRL_PngButton::SetCaptionColor(QRgb rc)
{
if (m_pLabel != NULL)
{
QString strColor;
strColor.asprintf("color:rgb(%d,%d,%d);",qRed(rc),qGreen(rc),qBlue(rc));
m_pLabel->setStyleSheet(strColor);
}
}
void YTEP_CTRL_PngButton::SetIconPath(QString path)
{
QString btnStyle = "QPushButton{border:none;background-image: url(" + path + ");border-radius:8px;}"
"QPushButton:hover{border:1px;color: rgb(255, 255, 255);background: rgb(0,162,232);background-image: url(" + path + ");border-radius:8px;}"
"QPushButton:pressed{border:2px;color: rgb(255, 255, 255);background: rgb(128,64,64);background-image: url(" + path + ");padding:2px 4px;border-radius:8px;}";
m_szIconPath = path;
if (m_pPushButton != NULL)
{
QPixmap icon1;//(tr(path.toLatin1()));
bool ret = icon1.load(path);
if (ret)
{
m_sizeIcon = icon1.size();
//m_pPushButton->setIcon(icon1);
m_pPushButton->setFixedSize(m_sizeIcon);
m_pPushButton->setStyleSheet(btnStyle);
}
else //加载默认图片
{
}
CalcLayout();
}
}
QString YTEP_CTRL_PngButton::GetCaption()
{
return m_szBtnCaption;
}
QString YTEP_CTRL_PngButton::GetIconPath()
{
return m_szIconPath;
}
QSize YTEP_CTRL_PngButton::CalcLayout()
{
m_sizeWidget.setWidth(m_sizeIcon.width() +
YTEP_PNG_WIDGET_BORDER * 2);
m_sizeWidget.setHeight(m_sizeIcon.height() +
YTEP_PNG_WIDGET_BORDER * 2 +
YTEP_PNG_WIDGET_SEP +
YTEP_PNG_WIDGET_LABEL);
m_rectIcon = QRect(0,0,m_sizeWidget.width(),m_sizeIcon.height() +
YTEP_PNG_WIDGET_BORDER * 2);
m_rectLabel = QRect(0,0,
m_rectIcon.width(),
YTEP_PNG_WIDGET_LABEL );
return m_sizeWidget;
}
void YTEP_CTRL_PngButton::LayoutControls()
{
CalcLayout();
this->setFixedSize(m_sizeWidget);
if (m_pPushButton != NULL)
{
m_pPushButton->resize(m_sizeIcon);
m_pPushButton->move(YTEP_PNG_WIDGET_BORDER,YTEP_PNG_WIDGET_BORDER);
}
if (m_pLabel != NULL)
{
m_pLabel->setGeometry(m_rectLabel);
m_pLabel->move(0,
YTEP_PNG_WIDGET_BORDER + m_sizeIcon.height() +
YTEP_PNG_WIDGET_SEP);
}
}
(2)、在MainWindows中实现连接:
m_pBtn01 = new YTEP_CTRL_PngButton(this);
m_pBtn02 = new YTEP_CTRL_PngButton(this);
m_pBtn03 = new YTEP_CTRL_PngButton(this);
m_pBtn04 = new YTEP_CTRL_PngButton(this);
m_pBtn05 = new YTEP_CTRL_PngButton(this);
m_pBtn06 = new YTEP_CTRL_PngButton(this);
m_pBtn07 = new YTEP_CTRL_PngButton(this);
m_pBtn08 = new YTEP_CTRL_PngButton(this);
//m_pBtn09 = new YTEP_CTRL_PngButton(this);
CreatePngButton(m_pBtn01,
":/image/res/001.png",
"实时监测");
CreatePngButton(m_pBtn02,
":/image/res/002.png",
"在线巡检");
CreatePngButton(m_pBtn03,
":/image/res/003.png",
"温度曲线");
CreatePngButton(m_pBtn04,
":/image/res/004.png",
"历史记录");
CreatePngButton(m_pBtn05,
":/image/res/005.png",
"温升故障");
CreatePngButton(m_pBtn06,
":/image/res/006.png",
"报警记录");
CreatePngButton(m_pBtn07,
":/image/res/007.png",
"设备管理");
CreatePngButton(m_pBtn08,
":/image/res/008.png",
"系统设置");
//CreatePngButton(m_pBtn09,
// ":/image/res/009.png",
// "退出系统");
QRect rcWindow(0,0,1024,768);
this->setGeometry(rcWindow);
//this->setWindowFlag(Qt::FramelessWindowHint);
connect(m_pBtn01,SIGNAL(clicked()),this,SLOT(OnBtn01_Clicked()));
connect(m_pBtn02,SIGNAL(clicked()),this,SLOT(OnBtn02_Clicked()));
connect(m_pBtn03,SIGNAL(clicked()),this,SLOT(OnBtn03_Clicked()));
connect(m_pBtn04,SIGNAL(clicked()),this,SLOT(OnBtn04_Clicked()));
connect(m_pBtn05,SIGNAL(clicked()),this,SLOT(OnBtn05_Clicked()));
connect(m_pBtn06,SIGNAL(clicked()),this,SLOT(OnBtn06_Clicked()));
connect(m_pBtn07,SIGNAL(clicked()),this,SLOT(OnBtn07_Clicked()));
connect(m_pBtn08,SIGNAL(clicked()),this,SLOT(OnBtn08_Clicked()));
(3)、信号处理的代码(MainWindow.h):
public slots:
void OnBtn01_Clicked();
void OnBtn02_Clicked();
void OnBtn03_Clicked();
void OnBtn04_Clicked();
void OnBtn05_Clicked();
void OnBtn06_Clicked();
void OnBtn07_Clicked();
void OnBtn08_Clicked();
(4)、信号实现代码:
void MainWindow::OnBtn01_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn02_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn03_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn04_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn05_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn06_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn07_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
void MainWindow::OnBtn08_Clicked()
{
QMessageBox::information(NULL,
"提示",
"运程对应程序!");
}
(5)、运行效果:
(6)、点击效果: