Qt-interest Archive, February 2007
Passing a parameter to a class in a .ui file ?
Message 1 in thread
I have a "boardwindow.ui" file that describes a QMainWindow
The code of this window is in a boardwindow.cpp :
BoardWindow::BoardWindow(QWidget * parent, Qt::WindowFlags flags, int
number_of_tiles )
: QMainWindow( parent, flags )
{
ui.setupUi(this);
}
In the .ui file, I have a custom widget called "board" of a class Board
which lies in board.cpp
Board::Board(QWidget *parent, QGraphicsScene *c, int number_of_tiles)
: QGraphicsView(c,parent)
{
doSometingWith(number_of_tiles);
}
Now the question is : since 'board' is created when all the widgets in the
ui file are created, the 'number_of-tiles' is not (yet) passed to 'board'
(it is set to the default value set in the .h definition)
I have tried to put in BoardWindow this code :
ui.setupUi(this);
ui.board = new Board(this, 0 , number_of_tiles);
but it does not work : the result is I still get the first board (the one
that is initialised at creation), and I get a new one, empty, in a corner
of the viewport.
Any idea as to how to do this ?
--
[ signature omitted ]
Message 2 in thread
Hello,
you cannot pass parameters through the constructor of the object when it
is created in the UI code. You have to set all your settings via
set-functions.
For example:
ui.setupUi(this);
ui.board->setNumberOfFiles(number_of_tiles);
The other alternative is to specify properties of your custom widget to
allow changing them in the Qt designer. Then you can simply set the
property "number_of_tiles" to something. But that does nothing else as
the first way.
Regards,
Falko
Am 18.02.2007 11:13, eb schrieb:
> I have a "boardwindow.ui" file that describes a QMainWindow
>
> The code of this window is in a boardwindow.cpp :
>
> BoardWindow::BoardWindow(QWidget * parent, Qt::WindowFlags flags, int
> number_of_tiles )
> : QMainWindow( parent, flags )
> {
>
> ui.setupUi(this);
> }
>
>
>
> In the .ui file, I have a custom widget called "board" of a class Board
> which lies in board.cpp
>
> Board::Board(QWidget *parent, QGraphicsScene *c, int number_of_tiles)
> : QGraphicsView(c,parent)
> {
>
> doSometingWith(number_of_tiles);
> }
>
>
>
> Now the question is : since 'board' is created when all the widgets in the
> ui file are created, the 'number_of-tiles' is not (yet) passed to 'board'
> (it is set to the default value set in the .h definition)
>
> I have tried to put in BoardWindow this code :
>
> ui.setupUi(this);
> ui.board = new Board(this, 0 , number_of_tiles);
>
> but it does not work : the result is I still get the first board (the one
> that is initialised at creation), and I get a new one, empty, in a corner
> of the viewport.
>
> Any idea as to how to do this ?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]
Message 3 in thread
Thanks both of you.
That was my fallback solution if my first intention was not feasible.
Falko Buttler wrote:
> Hello,
>
> you cannot pass parameters through the constructor of the object when it
> is created in the UI code. You have to set all your settings via
> set-functions.
> For example:
> ui.setupUi(this);
> ui.board->setNumberOfFiles(number_of_tiles);
>
> The other alternative is to specify properties of your custom widget to
> allow changing them in the Qt designer. Then you can simply set the
> property "number_of_tiles" to something. But that does nothing else as
> the first way.
>
> Regards,
> Falko
>
> Am 18.02.2007 11:13, eb schrieb:
>> I have a "boardwindow.ui" file that describes a QMainWindow
>>
>> The code of this window is in a boardwindow.cpp :
>>
>> BoardWindow::BoardWindow(QWidget * parent, Qt::WindowFlags flags, int
>> number_of_tiles )
>> : QMainWindow( parent, flags )
>> {
>>
>> ui.setupUi(this);
>> }
>>
>>
>>
>> In the .ui file, I have a custom widget called "board" of a class Board
>> which lies in board.cpp
>>
>> Board::Board(QWidget *parent, QGraphicsScene *c, int number_of_tiles)
>> : QGraphicsView(c,parent)
>> {
>>
>> doSometingWith(number_of_tiles);
>> }
>>
>>
>>
>> Now the question is : since 'board' is created when all the widgets in
>> the ui file are created, the 'number_of-tiles' is not (yet) passed to
>> 'board' (it is set to the default value set in the .h definition)
>>
>> I have tried to put in BoardWindow this code :
>>
>> ui.setupUi(this);
>> ui.board = new Board(this, 0 , number_of_tiles);
>>
>> but it does not work : the result is I still get the first board (the one
>> that is initialised at creation), and I get a new one, empty, in a corner
>> of the viewport.
>>
>> Any idea as to how to do this ?
>>
>> --
>> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
>> "unsubscribe" in the subject or the body. List archive and information:
>> http://lists.trolltech.com/qt-interest/
>>
>>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body. List archive and information:
> http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]
Message 4 in thread
QT 4.X has really gone away from complex constructors, and instead has
pushed towards a construct then initialize model.
I suggest you rewrite your widget to have a parent only constructor, and
then after setupUi, you initialize it with the values you want.
Scott
> -----Original Message-----
> From: eb [mailto:eb5@xxxxxxxxxx]
> Sent: Sunday, February 18, 2007 2:14 AM
> To: qt-interest@xxxxxxxxxxxxx
> Subject: Passing a parameter to a class in a .ui file ?
>
> I have a "boardwindow.ui" file that describes a QMainWindow
>
> The code of this window is in a boardwindow.cpp :
>
> BoardWindow::BoardWindow(QWidget * parent, Qt::WindowFlags flags, int
> number_of_tiles )
> : QMainWindow( parent, flags )
> {
>
> ui.setupUi(this);
> }
>
>
>
> In the .ui file, I have a custom widget called "board" of a class
Board
> which lies in board.cpp
>
> Board::Board(QWidget *parent, QGraphicsScene *c, int number_of_tiles)
> : QGraphicsView(c,parent)
> {
>
> doSometingWith(number_of_tiles);
> }
>
>
>
> Now the question is : since 'board' is created when all the widgets in
the
> ui file are created, the 'number_of-tiles' is not (yet) passed to
'board'
> (it is set to the default value set in the .h definition)
>
> I have tried to put in BoardWindow this code :
>
> ui.setupUi(this);
> ui.board = new Board(this, 0 , number_of_tiles);
>
> but it does not work : the result is I still get the first board (the
one
> that is initialised at creation), and I get a new one, empty, in a
corner
> of the viewport.
>
> Any idea as to how to do this ?
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with
> "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
--
[ signature omitted ]