PDA

View Full Version : First Program - expected class-name before '{' token



tnowacoski
28th January 2011, 20:40
I am sure this has been asked before but I am a newbie to QT and just cannot seem to figure this out. I have a small program with a mainwindow and a modal dialog. When I try to compile QT barks about expecting a class-name before '{' but my code appears to be the same as some working examples that I have seen?!? Can anyone help be figure out what I am doing wrong? Thanks!

Lykurg
28th January 2011, 21:01
Well you have more errors than that in your code. For the one you mentioned: Qt/C++ is case sensitive! It is not
Ui::Signonshould be
Ui::signon. The other errors are up to you ;)

tnowacoski
28th January 2011, 21:04
I know, I have messed around with this code so much, I am not even sure which way is up anymore.

tbscope
29th January 2011, 05:15
Qt is primarily a C++ api.
Therefor, you might want to start with basic C++ first.

tnowacoski
30th January 2011, 01:45
Can anyone suggest a place online where I can see and work through an example that uses multiple Ui's, and a main window? I have the C++ GUI Programming With QT4 book but to me there seem to be many holes in the teaching. I have programmed in VB for years and I am trying to make the transition to C++.

stampede
30th January 2011, 02:17
Basic usage:
main window examples (http://doc.qt.nokia.com/latest/examples-mainwindow.html)


(...) uses multiple Ui's, and a main window?
It's rather easy if you know how to create a widget that uses Ui form created with Designer.
Lets say that you have n forms, form_1.ui, form_2.ui, ..., form_n.ui.
foreach form f:
1) Add f into .pro file.
2) create and add to .pro a class that setups ui for f (include header "ui_form_n.h" and call 'setupUi()' ), example of n-th class (depends on names you use in .ui file, 'WidgetForm_n' is the name of main ui widget):


#ifndef _WIDGET_N_H__
#define _WIDGET_N_H__

#include <QWidget>
#include "ui_form_n.ui"

class Widget_n : public QWidget{
Q_OBJECT
public:
Widget_n( QWidget * parent = NULL ) : QWidget(parent){
_ui.setupUi(this);
}
protected:
Ui::WidgetForm_n _ui;
};

#endif

3) include headers for classes created in 2) in your mainWindow sources
4) call QMainWindow::setCentralWidget( new Widget_n() ) to set the widget representing n-th form as central for your main window at appropriate time.

tnowacoski
30th January 2011, 03:19
stampede:

You lost me at WidgetForm_n?...Suppose I have a mainwindow and a UI form named signon. I have added signon.ui to the .pro and created a signon.h and signon.cpp.

signon.h

#ifndef SIGNON_H
#define SIGNON_H

#include <QWidget>
#include "ui_signon.h"

class Signon : public QWidget{
Q_OBJECT
public:
Signon( QWidget * parent = NULL ) : QWidget(parent){
_ui.setupUi(this);
}
protected:
// What does here?
// Ui::MainWindow _ui;
};

#endif

signon.cpp:

#include <QtGui>
#include "signon.h"

Signon::Signon(QWidget *parent)
: QDialog(parent)
{
setupUi(this);
}

and mainwindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "signon.h"


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->hide();
ui::setCentralWidget( new Signon() );
}

MainWindow::~MainWindow()
{
delete ui;
}

What do I put in for
Ui::WidgetForm_n _ui;

in signon.h?

What am I doing wrong?

stampede
30th January 2011, 10:23
// What does here?
// Ui::MainWindow _ui;
Here goes your object from "ui_signon.h", you need to look how its named in ui file, because class name will be based on the object name you have entered in ui file, probably something like Ui::Signon _ui.

You have re-defined the constructor for Signon, I have defined it in header file just for example. You do either this:
.h

Signon( QWidget * parent = NULL ) : QWidget(parent){
_ui.setupUi(this);
}
or this:
.cpp

Signon::Signon(QWidget *parent)
: QDialog(parent)
{
setupUi(this); //!< should be '_ui.setupUi(this)'
}

And one more:

ui::setCentralWidget( new Signon() ); //!< should be this->setCentralW...

Do you know the meaning of "this" and "Ui::" statements ?

marcvanriet
31st January 2011, 11:33
Hi,

There have been a lot of posts about using 2 (or more) forms. You could also check out this FAQ :faq_qt_designer_2forms (http://www.qtcentre.org/faq.php?faq=qt_general_category#faq_qt_designer_2f orms).

But you should first try and do some programming with a single form.

You say there are some holes in "C++ GUI Programming With QT4". That is because the book is about learning Qt, and not about learning C++. You really really really really should first learn C++, and only then start to make something non-trivial in Qt.

Best regards,
Marc

tnowacoski
31st January 2011, 17:53
Agreed! I thought I could wing it but it is obvious that I need more references....Any suggestions on a good C++ book?

Added after 48 minutes:

Okay, after beating myself for being so dense, I finally saw the light and got this to work. While I am researching the best means of learning C++ I just have one more question. When this code runs, my second form "signon" paints first and only after that form is closed will my MainWindow paint. This form is modal to the MainWindow. How would I get the MainWindow to load first and the signon form to load on top?

MainWindow.cpp:

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "signon.h"


MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableWidget->hide();
openSignon();
}

MainWindow::~MainWindow()
{
delete ui;
}


void MainWindow::openSignon(){
Signon signon(this);
signon.exec();
}

marcvanriet
1st February 2011, 14:33
Hi,

You open the 'Signon' window in the constructor of MainWindow.

The constructor is code that is executed when the MainWindow object is created. This happens before showing the mainwindow. The mainwindow is only shown afterwards.

You must do openSignon() in a 'show event' or something that occurs after the main window is shown. This is done with the signal-slot mechanism. QtCreator allows you to easily generate code for a certain signal when you point at an object and do 'go to slot...'. Just make sure you understand this mechanism first.

Best regards,
Marc

tnowacoski
1st February 2011, 14:58
I figured that after I posted the message, was just wondering if there was another way.

Added after 5 minutes:

Thanks everyone for the posts! They really helped push me along. I am still open to suggestions about good C++ reference / tutorial material is anyone has an opinion! :)