Important: Please read the Qt Code of Conduct -

Reopen MainWindow

  • Hello,
    I made two mainwindow

    1. mainwindow.h & cpp
    2. playlist.h & cpp

    when i clicked open playlist button
    close mainwindow (1) and open playlist (2)

    but i don't know how to reopen mainwindow from playlist
    how to reopen ?

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QFileSystemModel>
    #include <QDebug>
    #include <QMediaPlayer>
    #include "playlist.h"
    #include "library.h"
    namespace Ui { class MainWindow; }
    class MainWindow : public QMainWindow
        MainWindow(QWidget *parent = nullptr);
        void show_main();
    private slots:
        void on_treeView_clicked(const QModelIndex &index);
        void on_progressSlider_sliderMoved(int position);
        void on_volumeSlider_sliderMoved(int position);
        void on_startBtn_clicked();
        void on_stopBtn_clicked();
        void on_positionChanged(int position);
        void on_durationChanged(int position);
        void on_playlistBtn_clicked();
        void on_libraryBtn_clicked();
        Ui::MainWindow *ui;
        QFileSystemModel *dirmodel;
        QFileSystemModel *filemodel;
        QMediaPlayer *player;
        QString abPath;
        PlayList *plyList;
        Library *lib;
    #endif // MAINWINDOW_H
    #ifndef PLAYLIST_H
    #define PLAYLIST_H
    #include <QMainWindow>
    #include "ui_mainwindow.h"
    namespace Ui {
        class PlayList;
    class PlayList : public QMainWindow
        explicit PlayList(QWidget *parent = nullptr);
    private slots:
        void on_playBtn_clicked();
        void on_stopBtn_clicked();
        void on_progressSlider_valueChanged(int value);
        void on_volumeSlider_valueChanged(int value);
        void on_mainBtn_clicked();
        void on_libBtn_clicked();
        void on_exitBtn_clicked();
        Ui::MainWindow *mainPtr;
        Ui::PlayList *ui;
    #endif // PLAYLIST_H

  • Lifetime Qt Champion

    @vel1024 Call
    Create an instance of it if already destroyed before calling show().
    But why do you want to have two main windows? You can have one and open more windows using simple QWidgets derived classes.

  • @jsulm

    thank you for your comment

    because, i'm a beginner so i don't know that i can't have two main windows

  • Lifetime Qt Champion

    @vel1024 I didn't say you can't. You can.
    But I would not create one than one main window unless I really need to.

  • @vel1024
    QMainWindow is actually nothing "different" or "special", it's just a window with a particular style. It is usual to only have one instance, but you can have multiple instances if you wish. Your interface sounds a little strange, but that's up to you. If you do decide to do this, you do not need to destroy and then recreate the windows, consider using hide() to hide and then show() to reshow them.

  • @JonB

    thank you for your comment

  • If your "myWindow" is just a window belong to the application you want to shut down. I'm afraid that we can hardly achieve that. We can't restart a window in a shuted down application LiteBlue

Log in to reply