Solved Can't receive signal from MainWindow from QLabel inherited class
-
Hello, everyone!
I'm making a project, where i need to interact with picture. So i inherited class from
QLabel
and promotedui->label
to it for this purpose. Now i can get mouse click position according to label (I transmit this data via signal toMainWindow
). And now comes the problem: I addedcomboBox
toui
and i want to send signal of it's item chosen fromMainWindow
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
-
Of cource that's not correct :) But I'm not sure what causes the SEGFAULT .
The best way is:
First, makevoid CustomLabel::process_combo_box(int)
public
Then connect it with comboxBox in the MainWindowconnect(ui->comboBox,SIGNAL(activated(int)), ui->label, SLOT(process_combo_box(int)));
No need to use MainWindow to send the signal.
-
Of cource that's not correct :) But I'm not sure what causes the SEGFAULT .
The best way is:
First, makevoid CustomLabel::process_combo_box(int)
public
Then connect it with comboxBox in the MainWindowconnect(ui->comboBox,SIGNAL(activated(int)), ui->label, SLOT(process_combo_box(int)));
No need to use MainWindow to send the signal.
-
@Bonnie , Perfect! That works! Thank you very much)
-
@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.