0

I am trying to build a Qt application where more than one window can occur.

Example: there is a main window and there is a settings menu on it. When settings menu is clicked, I want that another window will show up with the title Settings and will show different setting properties for the program.

I want this setting window to be designed in Qt creator rather than creating by writing code only. I have designed settings window and added the setings.ui file to my project.

I also created settingsWindow.cpp and settingsWindow.h following the structure of mainWindow.h and mainWindow.cpp ( which were generated automatically when I created the gui project).

But when I compile, it shows error on the ui(new Ui::settingsWindow) of settingsWindow constructor in settingsWindow.cpp

settingsWindow::settingsWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::settingsWindow)
{
    ui->setupUi(this);
}

The error says: 'Ui::settingsWindow' : no appropriate default constructor available

Here is the content of settingsWinow.h:

#ifndef SETTINGSWINDOW_H
#define SETTINGSWINDOW_H

#include <QMainWindow>

namespace Ui
{
    class settingsWindow;
}

class settingsWindow : public QMainWindow
{
    Q_OBJECT
public:
    explicit settingsWindow(QWidget *parent = 0);
    ~settingsWindow();

private:
    Ui::settingsWindow *ui;
};

#endif // SETTINGSWINDOW_H

What should I do?

Barshan Das
  • 3,677
  • 4
  • 32
  • 46

3 Answers3

2

You messed up somewhere in your window creation. If you want to use QtCreator only, you should :

File -> New File or Project ->Qt -> Qt Designer Form Class 

Then you will have a form you will be able to edit. The form will generate the class ui::settingsWindow. On the other side you will have a different class settingsWindow which have the same code as the one you show above.

ps: You will not have to worry about the class ui::settingsWindow. just make sure you have the directive which is missing from the sample code you provided

#include "ui_settingsWindow.h"
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
1

Assuming, that you created settingswindow class correctly(via right click on the forms list in project explorer ->add new -> qt -> qt designer form class, where you should implement your settingswindow class ) , you need to include settingsWindow.h in mainWindow.h, create in mainwindow.h pointer of settingsWindow class (if you want it to be global) and in mainwindow.cpp you just create it, like you would create QWidget for example, but without a parent.

mainwindow.h:

#include "settingsWindow.h"

... mainwindow class...

settingsWindow *sw;

.....

mainwindow.cpp:

... somewhere in constructor:
sw= new settingsWindow();
sw->show(); //(don't forget to delete sw in destructor, if you won't set any widget as it's parent)
Shf
  • 3,463
  • 2
  • 26
  • 42
0

More than likely there is a clash between your QMainWindow and the ui setup. Try changing to a QWidget and inheriting off both the Ui and the QWidget:

#ifndef SETTINGSWINDOW_H
#define SETTINGSWINDOW_H

#include <QWidget>
#include <QMainWindow>


namespace Ui
{
    class settingsWindow;
}

class settingsWindow : public QWidget, Ui::settingsWindow
{
    Q_OBJECT
public:
    explicit settingsWindow(QWidget *parent = 0);
    ~settingsWindow();
    QMainWindow* m_parent;

private:
    Ui::settingsWindow *ui;
};

and:

settingsWindow::settingsWindow(QWidget *parent) :
    m_parent(parent)
{
    ui->setupUi(this);
}
Phil Hannent
  • 12,047
  • 17
  • 71
  • 118