Important: Please read the Qt Code of Conduct -

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
    but it crashed my program.

    Here is some code from my .cpp file.

    (this is in the main function/constructor)

    lists = new QTabWidget;
        etfList = new QListWidget;
        ipoList = new QListWidget;
        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

    And in my header file:

    class MainWindow : public QMainWindow
        QTabWidget *lists;
        QListWidget *etfList;
        QListWidget *ipoList;
        explicit MainWindow(QWidget *parent = 0);
    private slots:
        void menuPopup(const QPoint &point);

  • Moderators

    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.

  • Lifetime Qt Champion

    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!

  • Lifetime Qt Champion

    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 :)

Log in to reply