Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Can't receive signal from MainWindow from QLabel inherited class

Can't receive signal from MainWindow from QLabel inherited class

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 322 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peter_Dev
    wrote on last edited by
    #1

    Hello, everyone!

    I'm making a project, where i need to interact with picture. So i inherited class from QLabeland promoted ui->label to it for this purpose. Now i can get mouse click position according to label (I transmit this data via signal to MainWindow). And now comes the problem: I added comboBox touiand i want to send signal of it's item chosen from MainWindow to that inherited class. But when i tried to do that i got SEGFAULT and my program was finished unexpectedly.

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include "customlabel.h"
    #include <QDebug>
    #include <QStandardItemModel>
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        connect(ui->label, SIGNAL(MousePressed()), this, SLOT(Mouse_current_pos()));
        connect(ui->comboBox,SIGNAL(activated(int)), this, SLOT(combo_choice(int))); //i think signal being send successfully
    
        ui->comboBox->addItem("Some menu");
        ui->comboBox->addItem("A");
        ui->comboBox->addItem("B");
        ui->comboBox->addItem("C");
        QStandardItemModel* model =
                qobject_cast<QStandardItemModel*>(ui->comboBox->model());
        QModelIndex firstIndex = model->index(0, ui->comboBox->modelColumn(),
                ui->comboBox->rootModelIndex());
        QStandardItem* firstItem = model->itemFromIndex(firstIndex);
        firstItem->setSelectable(false);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::Mouse_current_pos()
    {
        qDebug() << ui->label->x << " " << ui->label->y;
    }
    
    void MainWindow::combo_choice(int argument)
    {
        emit combo_box_chosen(argument);
    }
    

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class MainWindow; }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    private slots:
        void Mouse_current_pos();
        void combo_choice(int);
    private:
        Ui::MainWindow *ui;
    signals:
        void combo_box_chosen(int);
    };
    #endif // MAINWINDOW_H
    
    

    inherited from qlabel class customlabel.cpp

    #include "customlabel.h"
    #include "mainwindow.h"
    #include <QDebug>
    
    CustomLabel::CustomLabel(QWidget *parent) :
        QLabel(parent)
    {
        MainWindow a; //im not sure that this is correct. Segfault happens after i added these two lines
        connect(&a, SIGNAL(combo_box_chosen(int)), this, SLOT(process_combo_box(int)));
    }
    
    void CustomLabel::mousePressEvent(QMouseEvent *ev)
    {
        this->x = ev->x();
        this->y = ev->y();
        emit MousePressed();
    }
    
    void CustomLabel::process_combo_box(int argument)
    {
        qDebug() << argument;
    }
    
    

    customwidget.h

    #ifndef CUSTOMLABEL_H
    #define CUSTOMLABEL_H
    
    #include <QWidget>
    #include <QLabel>
    #include <QMouseEvent>
    #include <QDebug>
    
    class CustomLabel : public QLabel
    {
        Q_OBJECT
    public:
        explicit CustomLabel(QWidget *parent = nullptr);
    
        void mousePressEvent(QMouseEvent *ev);
        int x,y;
    private slots:
        void process_combo_box(int);
    signals:
        void MousePressed();
    };
    
    #endif // CUSTOMLABEL_H
    
    

    Any help would be appreciated

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      Of cource that's not correct :) But I'm not sure what causes the SEGFAULT .
      The best way is:
      First, make void CustomLabel::process_combo_box(int) public
      Then connect it with comboxBox in the MainWindow

          connect(ui->comboBox,SIGNAL(activated(int)), ui->label, SLOT(process_combo_box(int)));
      

      No need to use MainWindow to send the signal.

      P 1 Reply Last reply
      0
      • B Bonnie

        Of cource that's not correct :) But I'm not sure what causes the SEGFAULT .
        The best way is:
        First, make void CustomLabel::process_combo_box(int) public
        Then connect it with comboxBox in the MainWindow

            connect(ui->comboBox,SIGNAL(activated(int)), ui->label, SLOT(process_combo_box(int)));
        

        No need to use MainWindow to send the signal.

        P Offline
        P Offline
        Peter_Dev
        wrote on last edited by
        #3

        @Bonnie , Perfect! That works! Thank you very much)

        Pablo J. RoginaP 1 Reply Last reply
        0
        • P Peter_Dev

          @Bonnie , Perfect! That works! Thank you very much)

          Pablo J. RoginaP Offline
          Pablo J. RoginaP Offline
          Pablo J. Rogina
          wrote on last edited by
          #4

          @Peter_Dev in addition to your issue solved, you may want to use the new syntax for signals & slots, as it provides compile time errors, contrary to runtime warnings with the old syntax you're using.

          Upvote the answer(s) that helped you solve the issue
          Use "Topic Tools" button to mark your post as Solved
          Add screenshots via postimage.org
          Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved