新增多对象共用槽示例

master
feiyangqingyun 2021-12-25 15:14:04 +08:00
parent 4d377ad34a
commit 5c0c9637c4
23 changed files with 179 additions and 15 deletions

View File

@ -62,6 +62,7 @@
| other | 其他类别 | mouseline | 鼠标十字线 | | other | 其他类别 | mouseline | 鼠标十字线 |
| other | 其他类别 | ntpclient | NTP校时 | | other | 其他类别 | ntpclient | NTP校时 |
| other | 其他类别 | trayicon | 通用托盘效果 | | other | 其他类别 | trayicon | 通用托盘效果 |
| other | 其他类别 | multobj2slot | 多对象共用槽 |
| third | 第三方类 | designer | QtDesigner设计师(Qt4) | | third | 第三方类 | designer | QtDesigner设计师(Qt4) |
| third | 第三方类 | hotkey | 全局热键1 | | third | 第三方类 | hotkey | 全局热键1 |
| third | 第三方类 | shortcut | 全局热键2 | | third | 第三方类 | shortcut | 全局热键2 |
@ -142,6 +143,7 @@
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/mouseline.jpg) ![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/mouseline.jpg)
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/ntpclient.jpg) ![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/ntpclient.jpg)
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/trayicon.jpg) ![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/trayicon.jpg)
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/other/0snap/multobj2slot.jpg)
##### 4.7、第三方类 ##### 4.7、第三方类
![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/0snap/designer.jpg) ![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/0snap/designer.jpg)

Binary file not shown.

After

Width:  |  Height:  |  Size: 141 KiB

View File

@ -12,7 +12,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -0,0 +1,31 @@
#pragma execution_character_set("utf-8")
#include "widget.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk");
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QTextCodec::setCodecForLocale(codec);
#endif
Widget w;
w.setWindowTitle("多对象共用槽 (QQ: 517216493 WX: feiyangqingyun)");
w.show();
return a.exec();
}

View File

@ -0,0 +1,13 @@
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
TARGET = mouseline
TEMPLATE = app
DESTDIR = $$PWD/../bin
CONFIG += warn_off
SOURCES += main.cpp
SOURCES += widget.cpp
HEADERS += widget.h
FORMS += widget.ui

View File

@ -0,0 +1,70 @@
#include "widget.h"
#include "ui_widget.h"
#include "qpushbutton.h"
#include "qsignalmapper.h"
#include "qdatetime.h"
#include "qdebug.h"
#define TIMEMS QTime::currentTime().toString("hh:mm:ss zzz")
Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
{
ui->setupUi(this);
this->initBtn();
}
Widget::~Widget()
{
delete ui;
}
void Widget::initBtn()
{
QSignalMapper *signMap = new QSignalMapper(this);
connect(signMap, SIGNAL(mapped(QString)), this, SLOT(doBtn(QString)));
int x = 5, y = -25;
for (int i = 0; i < 1000; ++i) {
//动态设置坐标
x += 80;
if (i % 10 == 0) {
x = 5;
y += 30;
}
QPushButton *btn = new QPushButton(this);
btn->setObjectName(QString("btn_%1").arg(i + 1));
btn->setText(QString("text_%1").arg(i + 1));
btn->setGeometry(x, y, 75, 25);
//方法0: 每个按钮关联到一个独立的槽,代码量大不可取放弃
//方式1: 绑定到一个槽函数
connect(btn, SIGNAL(clicked(bool)), this, SLOT(doBtn()));
//方式2: 通过 QSignalMapper 转发信号
connect(btn, SIGNAL(clicked(bool)), signMap, SLOT(map()));
signMap->setMapping(btn, btn->objectName());
//方法3: 用 lambda 表达式
#if (QT_VERSION >= QT_VERSION_CHECK(5,6,0))
connect(btn, &QPushButton::clicked, [btn] {
QString name = btn->objectName();
qDebug() << TIMEMS << "doBtn3" << name;
});
connect(btn, &QPushButton::clicked, [=]() {
QString name = btn->objectName();
qDebug() << TIMEMS << "doBtn3" << name;
});
#endif
}
}
void Widget::doBtn()
{
QPushButton *btn = (QPushButton *)sender();
QString name = btn->objectName();
qDebug() << TIMEMS << "doBtn1" << name;
}
void Widget::doBtn(const QString &name)
{
qDebug() << TIMEMS << "doBtn2" << name;
}

View File

@ -0,0 +1,27 @@
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private:
Ui::Widget *ui;
private slots:
void initBtn();
void doBtn();
void doBtn(const QString &name);
};
#endif // WIDGET_H

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Widget</class>
<widget class="QWidget" name="Widget">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string/>
</property>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

View File

@ -6,3 +6,4 @@ SUBDIRS += mouseline
SUBDIRS += ntpclient SUBDIRS += ntpclient
SUBDIRS += trayicon SUBDIRS += trayicon
SUBDIRS += echartgauge SUBDIRS += echartgauge
SUBDIRS += multobj2slot

View File

@ -55,7 +55,7 @@ int main(int argc, char *argv[])
QDesigner app(argc, argv); QDesigner app(argc, argv);
app.setQuitOnLastWindowClosed(false); app.setQuitOnLastWindowClosed(false);
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -10,7 +10,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -10,7 +10,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -11,7 +11,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setWindowIcon(QIcon(":/main.ico")); a.setWindowIcon(QIcon(":/main.ico"));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -9,7 +9,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -11,7 +11,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -8,7 +8,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else

View File

@ -8,7 +8,7 @@ int main(int argc, char *argv[])
QApplication a(argc, argv); QApplication a(argc, argv);
a.setFont(QFont("Microsoft Yahei", 9)); a.setFont(QFont("Microsoft Yahei", 9));
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) #if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
#if _MSC_VER #if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("gbk"); QTextCodec *codec = QTextCodec::codecForName("gbk");
#else #else