Results 1 to 8 of 8

Thread: Problems with "destroyed" signal

  1. #1
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Question Problems with "destroyed" signal

    Greetings everyone!
    I have encountered a issue. I am trying to destroy a widget which was declared as an object in my mainwindow widget. I was hoping to get a "destroyed" signal in the mainwindow widget when the destruction take place - but I don't.
    Here is the code:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QObject>
    6. #include <QWidget>
    7. #include <QPushButton>
    8. #include <QBoxLayout>
    9. #include <QLabel>
    10. #include <QLineEdit>
    11. #include <QPointF>
    12. #include <QString>
    13. #include <QtSerialPort/QSerialPort>
    14. #include <QtSerialPort/QSerialPortInfo>
    15. #include <QComboBox>
    16. #include <QByteArray>
    17. #include <QDebug>
    18. #include <QListWidget>
    19. #include <QListWidgetItem>
    20. #include <QFile>
    21. #include <QTimer>
    22. #include <QDateTime>
    23. #include <QDir>
    24. #include <stddef.h>
    25. #include <stdint.h>
    26. #include <QGroupBox>
    27. #include <QMouseEvent>
    28. #include <QTextEdit>
    29. #include <math.h>
    30. #include <QClipboard>
    31. #include <QMimeData>
    32. #include <QApplication>
    33. #include <QDoubleSpinBox>
    34. #include <QSpinBox>
    35. #include "receiver.h"
    36. #include "transmitter.h"
    37.  
    38. namespace Ui {
    39. class MainWindow;
    40. }
    41.  
    42. class MainWindow : public QMainWindow
    43. {
    44. Q_OBJECT
    45.  
    46. public:
    47. explicit MainWindow(QWidget *parent = 0);
    48. ~MainWindow();
    49.  
    50. private:
    51. Ui::MainWindow *ui;
    52. Receiver *rec;
    53. Transmitter *tran;
    54. QPushButton *transmitter_button;
    55. QPushButton *receiver_button;
    56.  
    57. private slots:
    58. void receiver_slot(void);
    59. void transmitter_slot(void);
    60. void receiver_destroyed(void);
    61.  
    62. };
    63.  
    64. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef RECEIVER_H
    2. #define RECEIVER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QObject>
    6. #include <QWidget>
    7. #include <QPushButton>
    8. #include <QBoxLayout>
    9. #include <QDebug>
    10.  
    11. class Receiver : public QWidget
    12. {
    13. Q_OBJECT
    14. public:
    15. explicit Receiver(QWidget *parent = 0);
    16.  
    17. ~Receiver();
    18.  
    19. private:
    20. QPushButton *close;
    21.  
    22. signals:
    23. void close_sig(void);
    24.  
    25. public slots:
    26. void close_rec(void);
    27. //void abvgd(void);
    28. };
    29.  
    30. #endif // RECEIVER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef TRANSMITTER_H
    2. #define TRANSMITTER_H
    3.  
    4. #include <QMainWindow>
    5. #include <QObject>
    6. #include <QWidget>
    7.  
    8. class Transmitter : public QWidget
    9. {
    10. Q_OBJECT
    11. public:
    12. explicit Transmitter(QWidget *parent = 0);
    13. //~Transmitter();
    14.  
    15. private:
    16. //QPushButton *close;
    17.  
    18. signals:
    19.  
    20. public slots:
    21. //void close_tran(void);
    22. };
    23.  
    24. #endif // TRANSMITTER_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. transmitter_button = new QPushButton("Transmitter");
    10. receiver_button = new QPushButton("Receiver");
    11.  
    12. QHBoxLayout *horizontal_0 = new QHBoxLayout;
    13. horizontal_0->addWidget(transmitter_button);
    14. horizontal_0->addWidget(receiver_button);
    15.  
    16. ui->centralWidget->setLayout(horizontal_0);
    17.  
    18. connect(transmitter_button, SIGNAL(clicked(bool)), this, SLOT(transmitter_slot()));
    19. connect(receiver_button, SIGNAL(clicked(bool)), this, SLOT(receiver_slot()));
    20. connect(rec, SIGNAL(destroyed(QObject*)), this, SLOT(receiver_destroyed()));
    21. }
    22.  
    23. void MainWindow::transmitter_slot(void)
    24. {
    25. tran = new Transmitter;
    26. transmitter_button->setEnabled(false);
    27. tran->show();
    28. }
    29.  
    30. void MainWindow::receiver_slot(void)
    31. {
    32. rec = new Receiver;
    33. receiver_button->setEnabled(false);
    34. rec->show();
    35. }
    36.  
    37. void MainWindow::receiver_destroyed(void)
    38. {
    39. qDebug() << "ok";
    40. receiver_button->setEnabled(true);
    41. }
    42.  
    43. MainWindow::~MainWindow()
    44. {
    45. delete ui;
    46. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "receiver.h"
    2.  
    3. Receiver::Receiver(QWidget *parent) : QWidget(parent)
    4. {
    5. close = new QPushButton("Close");
    6.  
    7. QHBoxLayout *horizontal_0 = new QHBoxLayout;
    8. horizontal_0->addWidget(close);
    9.  
    10. this->setLayout(horizontal_0);
    11.  
    12. connect(close, SIGNAL(clicked(bool)), this, SLOT(close_rec()));
    13. }
    14.  
    15. void Receiver::close_rec(void)
    16. {
    17.  
    18. this->~Receiver();
    19. }
    20.  
    21. Receiver::~Receiver()
    22. {
    23.  
    24. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "transmitter.h"
    2.  
    3. Transmitter::Transmitter(QWidget *parent) : QWidget(parent)
    4. {
    5.  
    6. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Problems with "destroyed" signal

    You never connect to any object's destroyed() signal.
    You are quite luckly that your program doesn't crash when it accesses the uninitialized "rec" pointer.

    If you want to connect to the receiver object, then you have to put the connect() to where you actually have an object.

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Lightbulb Re: Problems with "destroyed" signal

    Thanks, but thats not enough - I have not understand what exactly I should do.
    How can I know in one widget that another one (which was created in the first one originally) was destroyed?

  4. #4
    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: Problems with "destroyed" signal

    Depends on what you need to do.

    If you just need to be aware of the object no longer existing, e.g. to create a new one instead, then you can just track the object with QPointer.

    If you need to do something with a window/dialog when it gets closed, i.e. when you still need access to the object itself, then make it emit a custom signal that you connect to.

    If you need to do something when the object is destroyed but don't need to access the object itself anymore, then you can connect to the destroyed() signal.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Problems with "destroyed" signal

    The last two methods, you mentioned - I have done them. I connected a SLOT in my mainwindow to "destroyed" signal and I also tried to connect a SLOT in mainwindow to a custom signal which is generating by the rec widget when getting destroyed. But the slot was never called((
    Now I wonder - why)
    I will look forward to try the QPointer you advised today though.
    Anyway I need to If you have any Idea why my mainwindow do not see the signals from the rec object?
    thank you))

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problems with "destroyed" signal

    Quote Originally Posted by Alexander111122 View Post
    I connected a SLOT in my mainwindow to "destroyed" signal
    You connect a "destroyed()" signal from a non-object pointed to by the unintialised rec pointer (Listing 4 line 20). This is unlikely to have any useful outcome, as anda_skoa has already said. rec is not initialised until after someone clicks the receive button. Until then rec is undefined (maybe nullptr, maybe something more dangerous) and cannot be connect()ed to anything.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  7. #7
    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: Problems with "destroyed" signal

    Quote Originally Posted by Alexander111122 View Post
    The last two methods, you mentioned - I have done them. I connected a SLOT in my mainwindow to "destroyed" signal and I also tried to connect a SLOT in mainwindow to a custom signal which is generating by the rec widget when getting destroyed.
    No you did not, at least not in the code that you've posted.

    Quote Originally Posted by Alexander111122 View Post
    Anyway I need to If you have any Idea why my mainwindow do not see the signals from the rec object?
    I already told you in comment #2.

    But you claim that you do something that the code you posted does not do, so it is hard to tell if that actually is what your code looks like or if the code you are actually working with does what you claim it does.

    Cheers,
    _

  8. #8
    Join Date
    Feb 2016
    Posts
    11
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: Problems with "destroyed" signal

    Problem solved thanks to anda_skoa!
    I have find out that I have not initialised the widget at first as I was told.
    Thank you very much my friends for provided help!

Similar Threads

  1. QOpenGLTexture error: "Texture has not been destroyed"
    By giordi in forum Qt Programming
    Replies: 0
    Last Post: 9th April 2015, 12:06
  2. Replies: 3
    Last Post: 16th March 2015, 07:31
  3. Replies: 0
    Last Post: 27th February 2010, 17:14
  4. Translation QFileDialog standart buttons ("Open"/"Save"/"Cancel")
    By victor.yacovlev in forum Qt Programming
    Replies: 4
    Last Post: 24th January 2008, 19:05
  5. Signal defined in "a.h" can not emit in "b.cpp"
    By Shawn in forum Qt Programming
    Replies: 9
    Last Post: 21st May 2007, 16:55

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.