Solved QDialog get parent info
-
My program has to QListwidgets. When I right click on the list I have a QDialog popup. When the QDialog pops up I would like to know which QListWidget has opened it. I tried using this line of code
qDebug()<<QDialog->parentWidget()->objectName();
but it crashed my program.Here is some code from my .cpp file.
(this is in the main function/constructor)
lists = new QTabWidget; lists->setFixedWidth(200); etfList = new QListWidget; ipoList = new QListWidget; etfList->setObjectName("ETFList"); etfList->setContextMenuPolicy(Qt::CustomContextMenu); ipoList->setObjectName("IPOList"); ipoList->setContextMenuPolicy(Qt::CustomContextMenu); lists->addTab(etfList, "ETF List"); lists->addTab(ipoList, "IPO List"); connect(etfList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &))); connect(ipoList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &)));
(this is the slot)
void MainWindow::menuPopup(const QPoint &point){ QDialog *dia = new QDialog(); qDebug()<<dia->parentWidget()->objectName(); <---crashes on this line dia->show(); } }
And in my header file:
class MainWindow : public QMainWindow { Q_OBJECT QTabWidget *lists; QListWidget *etfList; QListWidget *ipoList; public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); signals: private slots: void menuPopup(const QPoint &point);
-
@bandito
it crashes because you didn't set a parent on the dialog.
But trying to access it's parent pointer, which returns NULL but you calling objectName() on it.
So simply set a parent widget. E.g. when creating the dialog. -
Hi and welcome to devnet,
The usual way to get to know where the signal came from is to call sender.
-
@SGaist Thanks devnet. QObject::sender does the job!
-
You're welcome !
Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)