diff --git a/README.md b/README.md index d31fbac..4bb5a07 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,7 @@ | other | 其他类别 | mouseline | 鼠标十字线 | | other | 其他类别 | ntpclient | NTP校时 | | other | 其他类别 | trayicon | 通用托盘效果 | +| other | 其他类别 | multobj2slot | 多对象共用槽 | | third | 第三方类 | designer | QtDesigner设计师(Qt4) | | third | 第三方类 | hotkey | 全局热键1 | | 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/ntpclient.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、第三方类 ![avatar](https://gitee.com/feiyangqingyun/QWidgetDemo/raw/master/third/0snap/designer.jpg) diff --git a/other/0snap/multobj2slot.jpg b/other/0snap/multobj2slot.jpg new file mode 100644 index 0000000..584aeaa Binary files /dev/null and b/other/0snap/multobj2slot.jpg differ diff --git a/other/dbpage/main.cpp b/other/dbpage/main.cpp index e666386..3b0a430 100644 --- a/other/dbpage/main.cpp +++ b/other/dbpage/main.cpp @@ -12,7 +12,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/other/echartgauge/main.cpp b/other/echartgauge/main.cpp index 4b2a476..cbb2ba0 100644 --- a/other/echartgauge/main.cpp +++ b/other/echartgauge/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/other/lineeditnext/main.cpp b/other/lineeditnext/main.cpp index 92dc79f..e35d4b0 100644 --- a/other/lineeditnext/main.cpp +++ b/other/lineeditnext/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/other/mouseline/main.cpp b/other/mouseline/main.cpp index d88544c..65db2c8 100644 --- a/other/mouseline/main.cpp +++ b/other/mouseline/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/other/multobj2slot/main.cpp b/other/multobj2slot/main.cpp new file mode 100644 index 0000000..077b3ec --- /dev/null +++ b/other/multobj2slot/main.cpp @@ -0,0 +1,31 @@ +#pragma execution_character_set("utf-8") + +#include "widget.h" +#include +#include + +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(); +} diff --git a/other/multobj2slot/multobj2slot.pro b/other/multobj2slot/multobj2slot.pro new file mode 100644 index 0000000..0502909 --- /dev/null +++ b/other/multobj2slot/multobj2slot.pro @@ -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 diff --git a/other/multobj2slot/widget.cpp b/other/multobj2slot/widget.cpp new file mode 100644 index 0000000..88873c6 --- /dev/null +++ b/other/multobj2slot/widget.cpp @@ -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; +} diff --git a/other/multobj2slot/widget.h b/other/multobj2slot/widget.h new file mode 100644 index 0000000..9abf0f6 --- /dev/null +++ b/other/multobj2slot/widget.h @@ -0,0 +1,27 @@ +#ifndef WIDGET_H +#define WIDGET_H + +#include + +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 diff --git a/other/multobj2slot/widget.ui b/other/multobj2slot/widget.ui new file mode 100644 index 0000000..3227497 --- /dev/null +++ b/other/multobj2slot/widget.ui @@ -0,0 +1,20 @@ + + + Widget + + + + 0 + 0 + 800 + 600 + + + + + + + + + + diff --git a/other/other.pro b/other/other.pro index 97a141c..16069e4 100644 --- a/other/other.pro +++ b/other/other.pro @@ -6,3 +6,4 @@ SUBDIRS += mouseline SUBDIRS += ntpclient SUBDIRS += trayicon SUBDIRS += echartgauge +SUBDIRS += multobj2slot diff --git a/third/designer/main.cpp b/third/designer/main.cpp index 80dfb71..e62a417 100644 --- a/third/designer/main.cpp +++ b/third/designer/main.cpp @@ -55,7 +55,7 @@ int main(int argc, char *argv[]) QDesigner app(argc, argv); app.setQuitOnLastWindowClosed(false); -#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0)) +#if (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/countcode/main.cpp b/tool/countcode/main.cpp index 827f7c0..5767f4a 100644 --- a/tool/countcode/main.cpp +++ b/tool/countcode/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/emailtool/main.cpp b/tool/emailtool/main.cpp index 42b36cf..7d04704 100644 --- a/tool/emailtool/main.cpp +++ b/tool/emailtool/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/keydemo/main.cpp b/tool/keydemo/main.cpp index dda6fa1..85d82da 100644 --- a/tool/keydemo/main.cpp +++ b/tool/keydemo/main.cpp @@ -10,7 +10,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/keytool/main.cpp b/tool/keytool/main.cpp index 44b3dc7..f463491 100644 --- a/tool/keytool/main.cpp +++ b/tool/keytool/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/livedemo/main.cpp b/tool/livedemo/main.cpp index 7e9e6f1..5b0a0b4 100644 --- a/tool/livedemo/main.cpp +++ b/tool/livedemo/main.cpp @@ -10,7 +10,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/livetool/main.cpp b/tool/livetool/main.cpp index 685a7a2..5254301 100644 --- a/tool/livetool/main.cpp +++ b/tool/livetool/main.cpp @@ -11,7 +11,7 @@ int main(int argc, char *argv[]) QApplication a(argc, argv); 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 QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/tool/pngtool/main.cpp b/tool/pngtool/main.cpp index 0901d4f..e3cd0c4 100644 --- a/tool/pngtool/main.cpp +++ b/tool/pngtool/main.cpp @@ -9,7 +9,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/video/playffmpeg/main.cpp b/video/playffmpeg/main.cpp index a5e8fcc..d22ee79 100644 --- a/video/playffmpeg/main.cpp +++ b/video/playffmpeg/main.cpp @@ -11,7 +11,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/video/plaympv/main.cpp b/video/plaympv/main.cpp index cf29f5d..c29e982 100644 --- a/video/plaympv/main.cpp +++ b/video/plaympv/main.cpp @@ -8,7 +8,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else diff --git a/video/playvlc/main.cpp b/video/playvlc/main.cpp index c301696..840229a 100644 --- a/video/playvlc/main.cpp +++ b/video/playvlc/main.cpp @@ -8,7 +8,7 @@ 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 (QT_VERSION < QT_VERSION_CHECK(5,0,0)) #if _MSC_VER QTextCodec *codec = QTextCodec::codecForName("gbk"); #else