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. QDialog get parent info
Forum Updated to NodeBB v4.3 + New Features

QDialog get parent info

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 2.0k 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.
  • banditoB Offline
    banditoB Offline
    bandito
    wrote on last edited by
    #1

    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);
    
    
    raven-worxR 1 Reply Last reply
    0
    • banditoB bandito

      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);
      
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        The usual way to get to know where the signal came from is to call sender.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        banditoB 1 Reply Last reply
        0
        • SGaistS SGaist

          Hi and welcome to devnet,

          The usual way to get to know where the signal came from is to call sender.

          banditoB Offline
          banditoB Offline
          bandito
          wrote on last edited by
          #4

          @SGaist Thanks devnet. QObject::sender does the job!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            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