QDialog setVisible(false) crash



  • Hi, when i call function setVisible(false) in my QDialog i got this error:
    The program has unexpectedly finished.. Is this generally problem or i make mistake somewhere ?


  • Qt Champions 2016

    Hi
    Normally its not a reason to " unexpectedly finished"
    so maybe you can post you code so we can have a look?



  • chatdialog.cpp

    #include "chatdialog.h"
    #include "ui_chatdialog.h"

    chatDialog::chatDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::chatDialog)
    {
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose); //This enable destruktor
    this->setWindowFlags(Qt::WindowSystemMenuHint);
    createActions();
    createTrayIcon();
    connect(ui->messageTable->verticalScrollBar(),SIGNAL(rangeChanged(int,int)),this,SLOT(bottomScroll(int,int)));
    connect(sysTrayIcon,SIGNAL(messageClicked()),this,SLOT(messageClicked()));
    connect(sysTrayIcon,SIGNAL(activated(QSystemTrayIcon::ActivationReason)),this,SLOT(iconActivated(QSystemTrayIcon::ActivationReason)));
    sysTrayIcon->show();
    sysTrayIcon->setIcon(QIcon(":/image/logo.png"));
    visible=true;
    icon=QSystemTrayIcon::MessageIcon(QSystemTrayIcon::Information);

    minimizeAction->setEnabled(visible);
    hide->setEnabled(visible);
    maximizeAction->setEnabled(!isMaximized());
    restoreAction->setEnabled(isMaximized()||!visible);
    QDialog::setVisible(visible);
    
    
    ///////////////////////////////////////////////////////////////////////
    //// chat table design
    /// ///////////////////////////////////////////////////////////////////
    ///
    QPalette *palete=new QPalette;
    QBrush *brush=new QBrush;
    QPixmap pixmap=QPixmap::fromImage(QImage(":/image/logo.png").scaled(440,400));
    brush->setTexture(pixmap);
    palete->setBrush(QPalette::Base,*brush);
    ui->messageTable->setPalette(*palete);
    ui->messageTable->horizontalHeader()->setStretchLastSection(true);
    ui->messageTable->horizontalHeader()->setHidden(true);
    ui->messageTable->verticalHeader()->setHidden(true);
    ui->messageTable->setFrameShape(QFrame::NoFrame);
    ui->messageTable->setShowGrid(false);
    
    /////////////////////////////////////////////////////////////////////
    //// server thread
    ////////////////////////////////////////////////////////////////////
    

    // thread=new QThread;
    // server=new chatServer;
    // server->setUpServer(*thread);
    // server->moveToThread(thread);
    // thread->start();

    ////////////////////////////////////////////////////////////////
    //// Chat model
    /// ///////////////////////////////////////////////////////////
    stringList=new QStringList;
    stringListModel=new QStringListModel;
    QString message_to_chat="ajajajaj test";
    stringList->append(message_to_chat);
    stringListModel->setStringList(*stringList);
    
    ui->messageTable->setModel(stringListModel);
    
    
    
    ////////////////////////////////////////////////////////////////
    /// Scrol area setup
    ////////////////////////////////////////////////////////////////
    QWidget *widget=new QWidget;
    vlayout=new QVBoxLayout;
    widget->setLayout(vlayout);
    ui->scrollArea->setWidget(widget);
    QSpacerItem *spacer=new QSpacerItem(1,1,QSizePolicy::Expanding,QSizePolicy::Expanding);
    vlayout->addItem(spacer);
    
    ////////////////////////////////////////////////////////////////
    /// Client connection
    ///
    ///////////////////////////////////////////////////////////////
    client=new SocketClient(vlayout,stringListModel,stringList,ui->messageTable,sysTrayIcon);
    //client->Connect();
    

    }

    chatDialog::~chatDialog()
    {
    qDebug()<<"Form is close";
    emit closeForm();
    delete ui;
    delete client;
    thread->terminate();
    thread->deleteLater();
    server->deleteLater();
    delete stringList;
    delete stringListModel;

    }

    void chatDialog::createTrayIcon()
    {
    trayIconMenu=new QMenu(this);
    trayIconMenu->addAction(hide);
    trayIconMenu->addAction(minimizeAction);
    trayIconMenu->addAction(maximizeAction);
    trayIconMenu->addAction(restoreAction);
    trayIconMenu->addSeparator();
    trayIconMenu->addAction(quitAction);

    sysTrayIcon=new QSystemTrayIcon(this);
    sysTrayIcon->setContextMenu(trayIconMenu);
    

    }

    void chatDialog::createActions()
    {
    minimizeAction=new QAction(tr("Mi&nimize"),this);
    connect(minimizeAction,SIGNAL(triggered(bool)),this,SLOT(hide()));

    maximizeAction=new QAction(tr("Ma&ximize"),this);
    connect(maximizeAction,SIGNAL(triggered(bool)),this,SLOT(showMaximized()));
    
    restoreAction=new QAction(tr("&Restore"),this);
    connect(restoreAction,SIGNAL(triggered(bool)),this,SLOT(showNormal()));
    
    quitAction=new QAction(tr("&Quit"),this);
    connect(quitAction,SIGNAL(triggered(bool)),qApp,SLOT(quit()));
    
    hide=new QAction(tr("&Hide"),this);
    connect(hide,SIGNAL(triggered(bool)),this,SLOT(actionHide()));
    

    }

    void chatDialog::on_sendButton_clicked()
    {
    QPushButton *ja=new QPushButton("ja");
    vlayout->insertWidget(0,ja);
    }

    void chatDialog::on_messageLine_returnPressed()
    {

    }

    void chatDialog::bottomScroll(int x, int y)
    {
    qDebug()<<x<<y;
    ui->messageTable->verticalScrollBar()->setValue(y);
    }

    void chatDialog::messageClicked()
    {
    QMessageBox::information(0,tr("Systray"),tr("Ovo je poruka"));
    }

    void chatDialog::showMessage()
    {

    sysTrayIcon->showMessage("Test test test","Ovo je poruka1",icon,30000);
    

    }

    void chatDialog::iconActivated(QSystemTrayIcon::ActivationReason reason)
    {
    qDebug()<<"OVo je iconActivated";
    switch (reason) {
    case QSystemTrayIcon::Trigger:
    case QSystemTrayIcon::DoubleClick:
    break;
    case QSystemTrayIcon::MiddleClick:
    showMessage();
    break;
    default:
    ;
    }

    }

    void chatDialog::actionHide()
    {
    this->setVisible(false);
    }

    void chatDialog::closeEvent(QCloseEvent *event)
    {
    if (sysTrayIcon->isVisible()) {
    QMessageBox::information(this, tr("Systray"),
    tr("The program will keep running in the "
    "system tray. To terminate the program, "
    "choose <b>Quit</b> in the context menu "
    "of the system tray entry."));
    visible=false;
    this->setVisible(false);

         }
    event->ignore();
    

    }

    chatdialog.h

    #ifndef CHATDIALOG_H
    #define CHATDIALOG_H

    #include <QDialog>
    #include <QDebug>
    #include <QVBoxLayout>
    #include <QSpacerItem>
    #include "socketclient.h"
    #include "chatserver.h"
    #include <QThread>
    #include <QStringListModel>
    #include <QStringList>
    #include <QString>
    #include <QTextCursor>
    #include <QPalette>
    #include <QBrush>
    #include <QPixmap>
    #include <QImage>
    #include <QSystemTrayIcon>
    #include <QMenu>
    #include <QAction>
    #include <QMessageBox>
    #include <QCloseEvent>

    namespace Ui {
    class chatDialog;
    }

    class chatDialog : public QDialog
    {
    Q_OBJECT

    public:
    explicit chatDialog(QWidget *parent = 0);
    ~chatDialog();
    void createTrayIcon();
    void createActions();

    private:
    Ui::chatDialog *ui;
    QVBoxLayout *vlayout;
    chatServer *server;
    SocketClient *client;
    QThread *thread;
    QStringList *stringList;
    QStringListModel *stringListModel;
    QSystemTrayIcon *sysTrayIcon;
    QMenu *trayIconMenu;
    QAction *minimizeAction;
    QAction *maximizeAction;
    QAction *restoreAction;
    QAction *quitAction;
    QAction *hide;
    bool visible;

    QSystemTrayIcon::MessageIcon icon;
    

    signals:
    void closeForm();
    private slots:
    void on_sendButton_clicked();
    void on_messageLine_returnPressed();
    void bottomScroll(int x,int y);
    void messageClicked();
    void showMessage();
    void iconActivated(QSystemTrayIcon::ActivationReason reason);
    void actionHide();
    protected:
    void closeEvent(QCloseEvent *event);

    };

    #endif // CHATDIALOG_H


  • Qt Champions 2016

    @Sasha said:

    void chatDialog::closeEvent(QCloseEvent *event)
    ...
    this->setVisible(false);

    Is is this one that makes it crash ?

    You have this on
    this->setAttribute(Qt::WA_DeleteOnClose);

    If you disable it for test, it will still crash ?

    Also, you have
    delete ui; in chatDialog::~chatDialog()
    Is that ok? Normally it handled by ownership
    but you might have done something special.

    I will try later to compile when near compiler :)


  • Qt Champions 2016

    Hi
    Here it crashes in
    thread->terminate();
    in destructor

    Since you do not new it. (its a comment)
    // thread=new QThread;

    If I remove that, no crashes. But I did have to some stuff as I did not have the UI file.



  • @mrjj said:

    Since you do not new it. (its a comment)
    // thread=new QThread;

    If I remove that, no crashes. But I did have to some stuff as I did not have the UI file.

    Hi, it was problem with this line of code:
    this->setAttribute(Qt::WA_DeleteOnClose);
    But now my destructor does not work even if i close application. But thanks, u helped me a lot, i am now and i learning hard to get better.
    Once time, thanks a lot.


  • Qt Champions 2016

    Hi
    If you destructor is not being called, then you can just do it your
    self in the main windows destructor .
    Also,
    Please check where you new the chatDialog that you give it mainwin as parent
    like
    .. x= new chatDialog (this)

    and not just chatDialog() which gives it no parent and hence no one will delete it when application closes.



  • Hi, chatDialog is created by login dialog. And when login is success i create chatDialog and hide loginDialog. If i set parent for chatDialog then chatDialog will be hidden too. So i cant set parent for chatDialog...


  • Qt Champions 2016

    @Sasha
    Ok. I see.

    In Login Dialog
    new chatDialog (this)
    will give ChatDialog Login as parent, so
    its
    new chatDialog (this->parent())
    To give it main as parent.
    (Login->parent())



  • @mrjj said:

    In Login Dialog
    new chatDialog (this)
    will give ChatDialog Login as parent, so
    its
    new chatDialog (this->parent())
    To give it main as parent.

    It says:
    /home/guza/Chat/chatdialog.cpp:6: error: invalid conversion from 'QObject*' to 'QWidget*' [-fpermissive]
    ui(new Ui::chatDialog)
    Login is mainwindow..


  • Qt Champions 2016

    Ahh
    So login is the mainwinow.
    Well sorry then :)
    When you hide mainwin it hides its children.
    So you need to keep the ChatDialog pointer around and delete from
    Logins destructor.



  • Can u help me with thread and QTcpSocket. I have a thread class, and in that chass i have QTcpSocket object.
    When i write QByteArray with that QTcpSocket i got this message:
    Cannot create children for a parent that is in a different thread. (Parent is QNativeSocketEngine(0x7fa104003280), parent's thread is myThread(0xaf5e30), current thread is QThread(0xaf1210)
    In thread i have connect (DirectConnect) sockets signal readyRead and disconnects with threads slots readyRead and disconnects, and in parent of this thread i connect(AutoConnection) thread signal sendMessage with parent slots getMessage.
    But i got that error when i call in thread socket->write(array);


  • Qt Champions 2016

    @Sasha
    Hi you better make a new thread for that as it is a complex problem.
    http://stackoverflow.com/questions/6900994/qobject-cannot-create-children-for-a-parent-that-is-in-a-different-thread

    I never really used the network object so other here are far better to help you with this than me.



  • @mrjj said:

    me

    Thanks for everything, your help is priceless for me.


  • Qt Champions 2016

    @Sasha
    You are very welcome :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.