[SOLVED] Qt Creator/Designer: Blank Main Window
-
Hi,
Probably a dumb question, but it's my first app: http://developer.qt.nokia.com/doc/qt-4.8/designer-quick-start.html
As soon as I try to start the app, I see just a small blank window.
OS Linux, Qt Creator 2.4.0
-
Can you give your code to test it?
-
The small window you see when running your application is a normal, empty MainWindow. Add your UI to it if you want to actually see it.
-
[quote author="Tobias Hunger" date="1327398398"]The small window you see when running your application is a normal, empty MainWindow. Add your UI to it if you want to actually see it.[/quote]
But what exactly is this tiny step? Is it necessarily manual? The step-by-step guide fails to mention it. Press Ctrl+R and watch the fun, they say.
-
The tutorial is written for Qt Designer, you seem to be using Qt Creator, which has similar functionality built-in.
In Qt Designer Ctrl-R is the key combo to trigger a preview. That is used to run your project in Qt Creator. Try hitting Alt-Shift-R in Qt Creator instead.
-
[quote author="Tobias Hunger" date="1327405815"]Try hitting Alt-Shift-R in Qt Creator instead.[/quote]
Alt-Shift-R works, thanks! But why is the UI disconnected when I start it as an app?I've been studying this demo: http://www.youtube.com/watch?v=JtyCM4BTbYo
There's some curious difference:
Mine mainwindow.cpp:
@#include "mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
}MainWindow::~MainWindow()
{}
@His:
@#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}
@His variant seems to be generated; in my case it would have taken some tampering with (re)generated files, which is obviously no good.
Besides, my ui_mainwindow.h gets generated into ...-build-desktop-Qt_in_PATH_Debug directory. Is it OK?
It feels like some project wizard option has been chosen wrong. -
Your UI is disconnected since there is no code that actually adds your UI to the MainWindow.
"His" code does just that... Note that there is some definition of "ui" in his mainwindow.h that is also needed.
Any files that will be regenerated tend to have a comment right at the top, saying just that. Keep away from those, but feel free to edit all the others. They might be generated, but they are meant to be edited by you and will not get regenerated later.
That ui_mainwindow.h gets put into the build directory (...-build-...) is called shadow building. This means that all generated files will end up in the build directory, keeping your source directory clean. This is what should happen:-)
-
[quote author="Tobias Hunger" date="1327413569"]Your UI is disconnected since there is no code that actually adds your UI to the MainWindow.
"His" code does just that... Note that there is some definition of "ui" in his mainwindow.h that is also needed.
Any files that will be regenerated tend to have a comment right at the top, saying just that.[/quote]
I still can't figure the thing out. I tried this:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtGui/QMainWindow>
class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget parent = 0);
~MainWindow();
void ui;
};#endif // MAINWINDOW_H
@mainwindow.cpp:
@#include "mainwindow.h"
#include"ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui = new Ui::MainWindow;
static_castUi::MainWindow*(ui)->setupUi(this);
}MainWindow::~MainWindow()
{
delete static_castUi::MainWindow*(ui);
}
@It looks ugly, and it won't work. Compilation errors in ui_mainwindow.h.
I've got some trouble believing that the matter is considered too trivial to be mentioned in a tutorial for dummies.
-
Use Ui::MainWindow * as type for ui (line 13 of mainwindow.h). That saves you from doing all those ugly casts. Way less ugly that way:-)
Is there a ui_mainwindow.h somewhere? Try a clean rebuild. It is hard to say what is wrong without the error message.
-
[quote author="Tobias Hunger" date="1327418625"]Is there a ui_mainwindow.h somewhere? Try a clean rebuild. It is hard to say what is wrong without the error message.[/quote]
Yes, there is. Clean rebuild and all. I've made some progress:
@#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QtGui/QMainWindow>
namespace Ui
{
class MainWindow;
}class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
MainWindow(QWidget parent = 0);
~MainWindow();
private:
Ui::MainWindow ui;
};#endif // MAINWINDOW_H
@
@#include "mainwindow.h"
#include"ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}
@Still, there's an error: "Undefined reference to 'QWebView::QWebView(QWidget*)' in ui_mainwindow.h"
There is
@#include <QtWebKit/QWebView>@in ui_mainwindow.h
-
To wrap it up:
(1) If you need something esoteric, like QtWebKit, you'll have to put it into your project:
@#-------------------------------------------------Project created by QtCreator 2012-01-24T22:17:48
#-------------------------------------------------
QT += core gui webkit
TARGET = MiBrowser
TEMPLATE = appSOURCES += main.cpp
mainwindow.cppHEADERS += mainwindow.h
FORMS += mainwindow.ui
@(2) If you postpone the creation of the main form, you'd better create Qt Designer Form Class rather than Qt Designer Form, when it comes to it.
Thanks, Tobias!