Unsolved I want to open a new window when a push button is clicked but it gets it on the main window
-
void MainWindow::on_addhabit_clicked()
{addhabit *habitwindow=new addhabit(this,this);
habitwindow->show();
}
in the main window when addhabit button is clicked it should open a new different window but instead it open it on main window that both there items appear together -
@Rahma said in I want to open a new window when a push button is clicked but it gets it on the main window:
addhabit *habitwindow=new addhabit(this,this);
I guess addhabit is a QWidget?
Simply do not pass parent (this) to it then it will be a window.
See https://doc.qt.io/qt-5/qwidget.html#QWidget :
"If parent is 0, the new widget becomes a window. If parent is another widget, this widget becomes a child window inside parent." -
@jsulm
ohhhhh i get it now ...... thanks -
addhabit *habitwindow=new addhabit(this,this);
But if you don't pass a parent (this) please keep in mind that you'll need to manage your memory allocation (new) yourself...
-
This post is deleted! -
@Rahma you need to share more of your code, sharing just snippets is making others harder...
As you posted before:
void MainWindow::on_addhabit_clicked() { ...
that will work if you have a QPushButton object called addhabit also defined in your MainWindow class
-
The easiest and IMOH cleanest solution is to give the underlying QWidget Constructor the Window tag
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget w0; w0.show(); QWidget *w = new QWidget(&w0, Qt::Window); w->show(); w->resize(200,200); w->raise(); return a.exec(); }