Results 1 to 8 of 8

Thread: QDialog - get parent name

  1. #1
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default QDialog - get parent name

    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
    Qt Code:
    1. qDebug()<<QDialog->parentWidget()->objectName();
    To copy to clipboard, switch view to plain text mode 
    but it crashed my program.

    Here is some code from my .cpp file.

    (this is in the main function/constructor)
    Qt Code:
    1. lists = new QTabWidget;
    2. lists->setFixedWidth(200);
    3. etfList = new QListWidget;
    4. ipoList = new QListWidget;
    5. etfList->setObjectName("ETFList");
    6. etfList->setContextMenuPolicy(Qt::CustomContextMenu);
    7. ipoList->setObjectName("IPOList");
    8. ipoList->setContextMenuPolicy(Qt::CustomContextMenu);
    9. lists->addTab(etfList, "ETF List");
    10. lists->addTab(ipoList, "IPO List");
    11.  
    12.  
    13. connect(etfList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &)));
    14. connect(ipoList, SIGNAL(customContextMenuRequested(const QPoint &)), this,SLOT(menuPopup(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 

    (this is the slot)

    Qt Code:
    1. void MainWindow::menuPopup(const QPoint &point){
    2.  
    3. QDialog *dia = new QDialog();
    4. qDebug()<<dia->parentWidget()->objectName(); <---crashes on this line
    5. dia->show();
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 


    And in my header file:


    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4. QTabWidget *lists;
    5. QListWidget *etfList;
    6. QListWidget *ipoList;
    7.  
    8. public:
    9. explicit MainWindow(QWidget *parent = 0);
    10.  
    11. ~MainWindow();
    12.  
    13. signals:
    14.  
    15. private slots:
    16. void menuPopup(const QPoint &point);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog - get parent name

    qDebug()<<dia->parentWidget()->objectName(); <---crashes on this line
    Of course it does, because on the line before that you created your QDialog instance with no parent, so parentWidget() returns NULL. And by the way, your method of creating the QDialog on the heap (using "new") causes a memory leak. The more common method is to create temporary QDialog instances on the stack, which will automatically be destroyed when the slot exits and the QDialog instance goes out of scope.

    Anyway, it isn't the QDialog's parent you are interested in - that has nothing to do with which QListWidget issued the signal that the slot is handling. Look at the QObject::sender() method. When you are in the slot, it tells you which QObject issued the signal that your slot is handling. If the only signals connected to that slot are those from the QListWidget instances, then you can qobject_cast() the pointer returned by the sender() method to QListWidget * and see which one of your two QListWidget pointers it matches.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QDialog - get parent name

    Quote Originally Posted by d_stranz View Post
    The more common method is to create temporary QDialog instances on the stack, which will automatically be destroyed when the slot exits and the QDialog instance goes out of scope.
    Not for a non-modal dialog though, like in this case (QDialog::show() being called).
    For those it is usually either explicit delete or using the DeleteOnClose widget attribute.

    Cheers,
    _
    Last edited by anda_skoa; 24th August 2016 at 16:25.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog - get parent name

    Not for a non-modal dialog though, like in this case (QDialog::show() being called).
    For those it is usually either explicit delete or using the DeleteOnClose widget attribute.
    True, but I don't think that was the intent in this case. The code pattern implies that the QDialog instance was intended to be temporary, since neither the DeleteOnClose attribute was set nor the destructor called.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: QDialog - get parent name

    Thank you d_stranz and anda_skoa. The QObject::sender() works perfect. As for the temporary instances, anda_skoa is correct. Creating a temporary instance
    Qt Code:
    1. QDialog *dia;
    To copy to clipboard, switch view to plain text mode 
    ends up crashed the program but using
    Qt Code:
    1. dia->setAttribute(Qt::WA_DeleteOnClose, true);
    To copy to clipboard, switch view to plain text mode 
    runs with our a flaw.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog - get parent name

    QDialog *dia;
    This is not "creating a temporary instance". This is a declaration of a (uninitialized) local variable named "dia" of type "QDialog *". And of course, it crashes too, because it is uninitialized and points to garbage.

    This is what I meant:

    Qt Code:
    1. void MyClass::on_some_slot()
    2. {
    3. QDialog dlg;
    4. if ( dlg.exec() == QDialog::Accepted )
    5. {
    6. // do something with the result
    7. }
    8.  
    9. // dlg goes out of scope when method exits, and is automatically destroyed
    10. }
    To copy to clipboard, switch view to plain text mode 
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Mar 2016
    Posts
    18
    Thanks
    2
    Thanked 1 Time in 1 Post
    Qt products
    Qt5

    Default Re: QDialog - get parent name

    Quote Originally Posted by d_stranz View Post

    This is what I meant:

    Qt Code:
    1. void MyClass::on_some_slot()
    2. {
    3. QDialog dlg;
    4. if ( dlg.exec() == QDialog::Accepted )
    5. {
    6. // do something with the result
    7. }
    8.  
    9. // dlg goes out of scope when method exits, and is automatically destroyed
    10. }
    To copy to clipboard, switch view to plain text mode 
    Sorry d_stranz for the confusion.

    I tried your code but it does not enter the if condition. It does however run fine outside the if condition (Or appears to anyway).

    Maybe there us something else that I am missing?

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QDialog - get parent name

    I tried your code but it does not enter the if condition.
    Well, no, it won't as written. QDialog is like QWidget - it is a base class that contains nothing; you generally have to derive a class from it that you fill up with other GUI child widgets in a layout. In particular, classes derived from QDialog generally have an OK button or something like that which is connected to the QDialog::accept() slot. This causes the exec() function to exit with the QDialog::Accepted result code.

    If you execute the code I wrote above, the only way to close the dialog is to click the "X" button on the dialog's frame. This is connected to the QDialog::reject() slot. That causes the exec() method to exit with the QDialog::Rejected result code, so the if() clause is not entered. Change my code to replace "Accepted" with "Rejected" and see what happens.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. How to execute a qDialog without passing a parent
    By franco.amato in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2014, 22:22
  2. QDialog closing its parent on exit..
    By tescrin in forum Qt Programming
    Replies: 8
    Last Post: 19th December 2012, 01:38
  3. QDialog problem when setting mainwindow as parent
    By superpacko in forum Qt Programming
    Replies: 3
    Last Post: 21st July 2011, 10:35
  4. Force QDialog to appear just to left of parent?
    By Eos Pengwern in forum Qt Programming
    Replies: 2
    Last Post: 14th December 2009, 11:17
  5. Stop QDialog from inheriting parent's palette.
    By Avrohom in forum Qt Programming
    Replies: 3
    Last Post: 8th September 2009, 05:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.