2

I want to connect the clicked signal of some abstract buttons to a same slot. The thing is that it only works on one abstract button, when clicking on the others nothing occurs. Any ideas of what is failing?

I have already checked that the connection between the signal and the slot has been established correctly.

I have also tried to connect just one button at a time to see if the problem was connecting different buttons to the same slot, but the thing is that it only works with a specific button (with backroungLbl).

connect(ui.backgroundLbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam1mode1Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam1mode2Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam2mode2Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam1mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam2mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam3mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
connect(ui.cam4mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
void CIhm::backgroundClicked() {
    printf("background\n");
    if(ui.timePb->isVisible()){
        hideTimeBar();
    } else {
        showTimeBar();
    }
}
//Header
    class CIhm : public QWidget {
        Q_OBJECT
        private:
         class CCamWindow {
         public:
            QAbstractButton* window;
            QAbstractButton* name;

            CCamWindow(){
                window = NULL;
                name = NULL;
            }
            CCamWindow(QAbstractButton* w, QAbstractButton* i = NULL){
                window = w;
                name = i;
            }
         };

        public:
            CIhm();
            ~CIhm();

        private:
            std::map<int, std::vector<QX11EmbedContainer*> > m_ContainerMap;
            std::map<QX11EmbedContainer*, CCamWindow> m_ControlMap;

        private:
            void ConnectSlots();
            void CreateContainers();

        public slots:
            void backgroundClicked();

    };
#include "ihm.h"

CIhm::CIhm (){
    ConnectSlots();
    CreateContainers();

    ui.cam1mode4Lbl->hide();
    ui.cam2mode4Lbl->hide();
    ui.cam3mode4Lbl->hide();
    ui.cam4mode4Lbl->hide();
    ui.cam1mode4nameLbl->hide();
    ui.cam2mode4nameLbl->hide();
    ui.cam3mode4nameLbl->hide();
    ui.cam4mode4nameLbl->hide();
    ui.cam1mode1Lbl->hide();
    ui.cam1mode1nameLbl->hide();
}

void CIhm::ConnectSlots(){
    connect(ui.backgroundLbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam1mode1Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam1mode2Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam2mode2Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam1mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam2mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
    connect(ui.cam3mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundCLicked()));
    connect(ui.cam4mode4Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked()));
}

void CIhm::CreateContainers(){

    std::vector<QX11EmbedContainer*> singleContainer;
    singleContainer.push_back(new QX11EmbedContainer(this));
    singleContainer[0]->setGeometry(ui.cam1mode1Lbl->geometry().x(), ui.cam1mode1Lbl->geometry().y(), ui.cam1mode1Lbl->geometry().width()-1, ui.cam1mode1Lbl->geometry().height()-1);
    m_ControlMap[singleContainer[0]] = CCamWindow(ui.cam1mode1Lbl, ui.cam1mode1nameLbl);
    m_ContainerMap[1] = singleContainer;


    std::vector<QX11EmbedContainer*> doubleContainer;
    doubleContainer.push_back(new QX11EmbedContainer(this));
    doubleContainer[0]->setGeometry(ui.cam1mode2Lbl->geometry().x(), ui.cam1mode2Lbl->geometry().y(), ui.cam1mode2Lbl->geometry().width()-1, ui.cam1mode2Lbl->geometry().height()-1);
    m_ControlMap[doubleContainer[0]] = CCamWindow(ui.cam1mode2Lbl, ui.cam1mode2nameLbl);
    doubleContainer.push_back(new QX11EmbedContainer(this));
    doubleContainer[1]->setGeometry(ui.cam2mode2Lbl->geometry().x(), ui.cam2mode2Lbl->geometry().y(), ui.cam2mode2Lbl->geometry().width()-1, ui.cam2mode2Lbl->geometry().height()-1);
    m_ControlMap[doubleContainer[1]] = CCamWindow(ui.cam2mode2Lbl, ui.cam2mode2nameLbl);
    m_ContainerMap[2] = doubleContainer;


    std::vector<QX11EmbedContainer*> quadContainer;
    quadContainer.push_back(new QX11EmbedContainer(this));
    quadContainer[0]->setGeometry(ui.cam1mode4Lbl->geometry().x(), ui.cam1mode4Lbl->geometry().y(), ui.cam1mode4Lbl->geometry().width()-1, ui.cam1mode4Lbl->geometry().height()-1);
    m_ControlMap[quadContainer[0]] = CCamWindow(ui.cam1mode4Lbl, ui.cam1mode4nameLbl);
    quadContainer.push_back(new QX11EmbedContainer(this));
    quadContainer[1]->setGeometry(ui.cam2mode4Lbl->geometry().x(), ui.cam2mode4Lbl->geometry().y(), ui.cam2mode4Lbl->geometry().width()-1, ui.cam2mode4Lbl->geometry().height()-1);
    m_ControlMap[quadContainer[1]] = CCamWindow(ui.cam2mode4Lbl, ui.cam2mode4nameLbl);
    quadContainer.push_back(new QX11EmbedContainer(this));
    quadContainer[2]->setGeometry(ui.cam3mode4Lbl->geometry().x(), ui.cam3mode4Lbl->geometry().y(), ui.cam3mode4Lbl->geometry().width()-1, ui.cam3mode4Lbl->geometry().height()-1);
    m_ControlMap[quadContainer[2]] = CCamWindow(ui.cam3mode4Lbl, ui.cam3mode4nameLbl);
    quadContainer.push_back(new QX11EmbedContainer(this));
    quadContainer[3]->setGeometry(ui.cam4mode4Lbl->geometry().x(), ui.cam4mode4Lbl->geometry().y(), ui.cam4mode4Lbl->geometry().width()-1, ui.cam4mode4Lbl->geometry().height()-1);
    m_ControlMap[quadContainer[3]] = CCamWindow(ui.cam4mode4Lbl, ui.cam4mode4nameLbl);
    m_ContainerMap[4] = quadContainer;
}

void CIhm::backgroundClicked() {
    printf("background\n");
        if(ui.timePb->isVisible()){
            hideTimeBar();
        } else {
            showTimeBar();
        }
}
marpe
  • 185
  • 2
  • 9
  • that is weird.....how did you do this: ***checked that the connection between the signal and the slot has been established correctly.*** – ΦXocę 웃 Пepeúpa ツ Oct 30 '19 at 10:26
  • for example with cam1mode1Lbl y did: const bool connected = connect(ui.cam1mode1Lbl, SIGNAL(clicked()), this, SLOT(backgroundClicked())); and then I printed "connected" with WriteLog("Conexion established? = %d", connected); being the result 1 (true); – marpe Oct 30 '19 at 10:53
  • What is the type of `camXmodeX` objects ? – thibsc Oct 30 '19 at 11:11
  • camXmodeX as backgroundLbl are QAbstractButton class – marpe Oct 30 '19 at 11:15
  • And can we see the implementation of `backgroundClicked` please ? – thibsc Oct 30 '19 at 11:19
  • 1
    @marpe I don't believe you. You **cannot instantiate `QAbstractButton`** because the [`paintEvent()`](https://doc.qt.io/qt-5/qabstractbutton.html#paintEvent) method needs to be implemented (pure virtual). So it would not even compile. So you have something else, `QPushButton`s perhaps. Please show a minimal and reproducible example, because what you showed works (assuming you have a proper subclass of `QAbstractButton`). – Fareanor Oct 30 '19 at 11:26
  • @marpe, what Fareanor says is true, so give some informations please – thibsc Oct 30 '19 at 11:32
  • @Fareanor I have a subclass of QAbstractButton which is the one I have been using in all my project without having any problems till now. It compiles without errors but then whenever I touch my camXmodeX widgets it doesn´t do anything while when touching the background widget it works perfectly. (all of them widgets are of the same type) – marpe Oct 30 '19 at 11:34
  • @marpe So, you need to be accurate then. Moreover, the sample code you provided works perfectly fine. The issue comes from somewhere else in the code. **Please provide a complete, minimal and reproducible example**. While you refuse to provide it, no one will be able to help you. We are not supposed to guess your code (unless there is some black magic wizards here :) ) – Fareanor Oct 30 '19 at 11:37
  • @Fareanor the thing is that I wanted this widgets so as to reproduce camera video on them by using QX11 EmbedContainers. As regards the images visualization the project works well, but touching on the widgets didn´t do anything. That´s why I thought that when reproducing camera video on the widget I couldn´t touch the widget. Therefore I commented this part of the code and left just these slot connections to see whether they detect clicks or not, and they don´t work. – marpe Oct 30 '19 at 11:50
  • 1
    @marpe Connecting several signals from different objects to the same slot works for me (I already have done it in the past by the way). So no matter what are your assumptions, the code you showed works. We don't need your assumptions, we need a minimal, complete and reproducible example. If you don't provide what I'm asking to you, don't expect any help. That's all. Other thing, you should remove the old macros `SIGNAL` and `SLOT` and use the [new signal slot connection syntax](https://wiki.qt.io/New_Signal_Slot_Syntax) instead. – Fareanor Oct 30 '19 at 11:56
  • Do all involved `QObject` instances have the same thread affinity, in other words, have they all been created in the same thread? If not, that is likely to be the cause of your problem. – Ton van den Heuvel Oct 30 '19 at 12:02
  • I have already uploaded the basic part of my code. If I comment the "CreateContainers" in the constructor, cam1mode2 and cam2mode2 detect the clicks, otherwise they don´t. If i discomment "CreateContainers" my project works with the exception that camXmodeX don´t recognize the clicks. – marpe Oct 30 '19 at 12:56

0 Answers0