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. Memory problem and strange behaviour

Memory problem and strange behaviour

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.4k 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.
  • S Offline
    S Offline
    Saroupille
    wrote on last edited by
    #1

    Hi,

    I'm quite new with Qt, and I have some issues with my program. Some executions end without any errors, and others don't.

    Sometimes, the debug consol print a lot of lines, like there are a memory leak, sometimes the app stop promptly. I don't know why, I started to observ this behaviour after adding a layout on the "preference window".

    This is my code :
    main.cpp
    @
    #include <QtWidgets>
    #include <QLineEdit>
    #include <QToolButton>
    #include <MainWindow.h>

    int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    MainWindow window;
    window.show();
    return app.exec();
    }
    @
    MainWindow.h
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtWidgets>
    #include <QMenu>
    #include <QToolBar>
    #include <QTabWidget>
    #include <QAction>

    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow();
    private slots:
    void newWindow();
    void openPreferencesMenu();
    private:
    /*
    QMenu *fileMenu;
    QToolBar *fileToolBar;
    QToolBar *editToolBar;
    */
    QMenu *fileMenu;
    QMenu *editMenu;

    QAction *newAct;
    QAction *editAct;
    

    };

    #endif // MAINWINDOW_H@

    PreferenceWindow.h
    @#define PREFERENCEWINDOW_H

    #include <QtWidgets>

    class PreferenceWindow : public QDialog
    {
    Q_OBJECT
    public:
    PreferenceWindow();
    private:
    QLineEdit *programPath;
    QLabel *programPathLabel;

    QLineEdit *programDefaultOptions;
    QLabel * programDefaultOptionsLabel;
    
    QFormLayout *layout;
    QPushButton *acceptButton;
    QPushButton *cancelButton;
    

    };

    #endif // PREFERENCEWINDOW_H@

    MainWIndow.cpp
    @#include <PreferenceWindow.h>
    MainWindow::MainWindow() {
    fileMenu = menuBar()->addMenu(tr("&File"));
    editMenu = menuBar()->addMenu(tr("&Edit"));
    newAct=new QAction(QIcon("images/new.png"), tr("&New"), this);
    newAct->setShortcuts(QKeySequence::New);
    newAct->setStatusTip(tr("Create a new file"));
    editAct = new QAction(tr("&Edit"), this);
    editAct->setShortcuts(QKeySequence::Preferences);
    editAct->setStatusTip(tr("Set preferences"));
    connect(newAct, SIGNAL(triggered()), this, SLOT(newWindow()));
    connect(editAct, SIGNAL(triggered()), this, SLOT(openPreferencesMenu()));
    fileMenu->addAction(newAct);
    editMenu->addAction(editAct);

    }

    void MainWindow::openPreferencesMenu() {
    PreferenceWindow *p = new PreferenceWindow();
    }

    void MainWindow::newWindow() {

    MainWindow *w=new MainWindow();
    w->setAttribute(Qt::WA_DeleteOnClose);
    w->move(x()-10,y()-10);
    w->show();
    

    }@

    PreferenceWindow.cpp
    @#include <PreferenceWindow.h>
    #include <QHBoxLayout>
    PreferenceWindow::PreferenceWindow() {

    this->setModal(true);
    programPath= new QLineEdit(this);
    programPathLabel=new QLabel(tr("program path : "), this);
    layout=new QFormLayout();
    
    programDefaultOptions=new QLineEdit(this);
    programDefaultOptionsLabel=new QLabel(tr("Default options :"),this);
    acceptButton = new QPushButton(tr("Accept"));
    cancelButton = new QPushButton(tr("Cancel"));
    
    layout->addRow(programPathLabel, programPath);
    layout->addRow(programDefaultOptionsLabel, programDefaultOptions);
    layout->addRow(acceptButton, cancelButton);
    
    this->setLayout(layout);
    connect(acceptButton, SIGNAL(clicked()), this, SLOT(accept()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
    
    this->show();
    

    }
    @

    Thanks,

    Saroupille.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Colon
      wrote on last edited by
      #2

      Create PreferenceWindow in MainWIndow.cpp like this:

      pwin = new PreferenceWindow();
      pwin->setHidden(true);

      and the SOLT: openPreferencesMenu()
      void MainWindow::openPreferencesMenu() {
      pwin->show();
      }

      1 Reply Last reply
      0
      • S Offline
        S Offline
        Saroupille
        wrote on last edited by
        #3

        Thanks, it seems to work !

        But I don't really understand why my method caused this error. And why your method is better ?

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Colon
          wrote on last edited by
          #4

          the compiler should tell you
          with your code you get:
          warning: unused variable 'p' [-Wunused-variable]
          PreferenceWindow *p = new PreferenceWindow();
          ^
          you create the window as local variable

          1 Reply Last reply
          0
          • S Offline
            S Offline
            Saroupille
            wrote on last edited by
            #5

            Indeed, thanks !

            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