PDA

View Full Version : How to initiate a form based on QUiLoader



peterjb31
21st December 2010, 16:00
Hi all

I'm relatively new to both C++ and QT and I am trying to create an application which dynamically loads a form based on what is returned from a query to a database. Below is the code I am using to try and 'run' the form:


ItemAdd item;
item.show();

This however doesn't seem to do anything. The contents of ItemAdd.cpp are as follows:

#include "itemadd.h"

ItemAdd::ItemAdd(QWidget *parent) :
QWidget(parent)
{
QUiLoader builder;
QFile file("form.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = builder.load(&file, this);
file.close();

QPushButton *tmpButton = new QPushButton;
tmpButton = qFindChild<QPushButton*>(this,"addNewItemButton");
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(myWidget);
setLayout(layout);
// connect(tmpButton,SIGNAL(clicked()),this,SLOT(slot AddItem()));
}
void ItemAdd::slotAddItem() {
// Code to be added later;
}

ItemAdd::~ItemAdd() {

}

I have based both my header and cpp file on the defaults for a QDialog form created by Qt Creator. Below is my header file:



#ifndef ITEMADD_H
#define ITEMADD_H
#include <QtUiTools>
#include <QtGui>
#include <QWidget>

namespace Ui {
class ItemAdd;
}

class ItemAdd : public QWidget
{
Q_OBJECT

public:
explicit ItemAdd(QWidget *parent = 0);
~ItemAdd();
void slotAddItem();
QString uifile;

private:
Ui::ItemAdd *ui;
};

#endif // ITEMADD_H



Many thanks for any help you can offer, I've looked through some documentation but can't seem to find what I need.

high_flyer
21st December 2010, 16:30
what is defined (which widgets) in your ui form?
If its an empty widget, you will only see and empty widget on your empty widget.

tbscope
21st December 2010, 16:37
And the widget is created on the stack. You might have deleted it already before it was able to show.

peterjb31
21st December 2010, 16:44
At the moment I have a single QPushButton on the form for use in testing.

Added after 6 minutes:


And the widget is created on the stack. You might have deleted it already before it was able to show.

Hi, I don't quite understand what created on the stack means, how would I make sure it isn't deleted?

Lykurg
21st December 2010, 18:22
As I see it, all needed widgets are created on the heap, only the builder is created on the stack. Which is fine.
But are you sure the path to the ui file is correct? I don't think so. Debug what
file.open(QFile::ReadOnly);says. Remember the relative path is relativ to the executable directory.

peterjb31
21st December 2010, 18:35
Hi Lykurg, thanks for your advice. qDebug returned true for that line though I looked at the code more closely around there afterwards. The examples all show the use of:

QFile file(":form.ui");
where as I had:

QFile file("form.ui");

Anyway, I changed my code to match the format of :form.ui and I am now getting the following error:

Designer: An error has occurred while reading the UI file at line 1, column 0: Premature end of document.
QLayout: Cannot add null widget to QVBoxLayout/

I have now tried several different .ui files but all provide the same error. Do you have any suggestions?

Many thanks.

Lykurg
21st December 2010, 23:54
Hi,

the ":" only indicates that it uses the resource system of Qt. You don't need that. Also your two line about tmpButton are nonsense. Anyway, I can't see a real problem in your code. Although you are mixing two approaches on how to use ui files when you also declare a ui pointer in the header file. (See the Qt Designer manual for a detailed description on how to use ui files)

But to the problem: what does
qWarning() << file.readAll(); say? (after opening it off course) It must be something with the file. It isn't read correctly...

peterjb31
22nd December 2010, 10:23
Hi, I tried to use the qWarning() function but just had:

"" printed each time on the console.

Could my problem be caused because I am trying to call the UI from a button click within an existing QWindow. I have got the code to load the form in the end by changing the following:


QUiLoader builder;
QFile file("form.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = builder.load(&file, this);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(myWidget);
setLayout(layout);

to


QUiLoader builder;
QFile file("../form.ui");
file.open(QFile::ReadOnly);
QWidget *myWidget = builder.load(&file);
myWidget->show();
file.close();

Is there anything wrong with the new code or can I settle on it?

Lykurg
22nd December 2010, 10:37
Hi, I tried to use the qWarning() function but just had:Yes, that is what I said in post #5: Your file is not loaded. Why: because you are using a relative path.
So: use the resource system or use an absolute path or use a correct relative path. (See also Current working directory)