How to initiate a form based on QUiLoader
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:
Code:
ItemAdd item;
item.show();
This however doesn't seem to do anything. The contents of ItemAdd.cpp are as follows:
Code:
#include "itemadd.h"
ItemAdd
::ItemAdd(QWidget *parent
) :{
file.
open(QFile::ReadOnly);
QWidget *myWidget
= builder.
load(&file,
this);
file.close();
tmpButton = qFindChild<QPushButton*>(this,"addNewItemButton");
layout->addWidget(myWidget);
setLayout(layout);
// connect(tmpButton,SIGNAL(clicked()),this,SLOT(slotAddItem()));
}
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:
Code:
#ifndef ITEMADD_H
#define ITEMADD_H
#include <QtUiTools>
#include <QtGui>
#include <QWidget>
namespace Ui {
class ItemAdd;
}
{
Q_OBJECT
public:
explicit ItemAdd
(QWidget *parent
= 0);
~ItemAdd();
void slotAddItem();
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.
Re: How to initiate a form based on QUiLoader
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.
Re: How to initiate a form based on QUiLoader
And the widget is created on the stack. You might have deleted it already before it was able to show.
Re: How to initiate a form based on QUiLoader
At the moment I have a single QPushButton on the form for use in testing.
Added after 6 minutes:
Quote:
Originally Posted by
tbscope
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?
Re: How to initiate a form based on QUiLoader
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
Code:
file.
open(QFile::ReadOnly);
says. Remember the relative path is relativ to the executable directory.
Re: How to initiate a form based on QUiLoader
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:
where as I had:
Anyway, I changed my code to match the format of :form.ui and I am now getting the following error:
Quote:
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.
Re: How to initiate a form based on QUiLoader
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
Code:
qWarning() << file.readAll();
say? (after opening it off course) It must be something with the file. It isn't read correctly...
Re: How to initiate a form based on QUiLoader
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:
Code:
file.
open(QFile::ReadOnly);
QWidget *myWidget
= builder.
load(&file,
this);
layout->addWidget(myWidget);
setLayout(layout);
to
Code:
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?
Re: How to initiate a form based on QUiLoader
Quote:
Originally Posted by
peterjb31
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 [WIKI]Current working directory[/WIKI])