You might set name to widgets and then discover them anywhere:
for(auto w_ptr: qApp->allWidgets())
if(w_ptr->objectName() == "QObject anywhere")
connect(...)
or in the parent widget:
if(QPushButton* o = findChild<QPushButton*>("QPushButton with name"))
connect(...)
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget wdgt;
wdgt.setWindowTitle("wdgt");
wdgt.setObjectName("wdgt");
wdgt.show();
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include <qlayout>
#include <qpushButton>
#include <qdebug>
#include <qapplication>
#include "widget.h"
#include "connect_by_name.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("Widget");
m_label->setFrameShape(QFrame::Box);
QHBoxLayout * hl{new QHBoxLayout{}};
hl->addWidget(m_label);
hl->addWidget(new connect_by_name{});
setLayout(hl);
connect_to_unique_pb();
}
void Widget
::connect_to_unique_pb() {
if(QPushButton * pb_ptr
= findChild<QPushButton*>("unique_pb"))
{
connect(pb_ptr, &QPushButton::pressed,
m_label, &QLabel::clear);
connect(pb_ptr, &QPushButton::released,
this, &Widget::pb_relased);
}
else
{
qDebug() << "The push button not found.";
}
}
void Widget
::pb_relased() {
m_label->setText("button not pressed");
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <qwidget>
#include <QLabel>
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
public slots:
void pb_relased();
private:
void connect_to_unique_pb();
private:
QLabel * m_label{new QLabel{"button not pressed"}};
};
#endif // WIDGET_H
connect_by_name.cpp
#include <qapplication>
#include <qdebug>
#include <qwidget>
#include "connect_by_name.h"
#include "widget.h"
connect_by_name
::connect_by_name(QWidget *parent) :
QWidget(parent)
{
m_pb->setObjectName("unique_pb");
m_hl->addWidget(m_pb);
connect_to_unique_widget();
}
void connect_by_name
::connect_to_unique_widget() {
for(auto w_ptr: qApp->allWidgets())
if(w_ptr->objectName() == "wdgt") {
connect(m_pb, &QPushButton::pressed,
w_ptr, &QWidget::hide);
connect(m_pb, &QPushButton::released,
w_ptr, &QWidget::show);
break;
}
}
connect_by_name.h
#ifndef CONNECT_BY_NAME_H
#define CONNECT_BY_NAME_H
#include <QWidget>
#include <QPushButton>
#include <QLayout>
class connect_by_name : public QWidget
{
Q_OBJECT
public:
explicit connect_by_name(QWidget *parent = nullptr);
private:
void connect_to_unique_widget();
private:
QHBoxLayout * m_hl {new QHBoxLayout{this}};
QPushButton * m_pb {new QPushButton{"unique button"}};
};
#endif // CONNECT_BY_NAME_H
connect.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = connect
TEMPLATE = app
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
widget.cpp \
connect_by_name.cpp
HEADERS += \
widget.h \
connect_by_name.h