Results 1 to 12 of 12

Thread: First Program - expected class-name before '{' token

  1. #1
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default First Program - expected class-name before '{' token

    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!
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: First Program - expected class-name before '{' token

    Well you have more errors than that in your code. For the one you mentioned: Qt/C++ is case sensitive! It is not
    Qt Code:
    1. Ui::Signon
    To copy to clipboard, switch view to plain text mode 
    should be
    Qt Code:
    1. Ui::signon
    To copy to clipboard, switch view to plain text mode 
    . The other errors are up to you

  3. #3
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First Program - expected class-name before '{' token

    I know, I have messed around with this code so much, I am not even sure which way is up anymore.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: First Program - expected class-name before '{' token

    Qt is primarily a C++ api.
    Therefor, you might want to start with basic C++ first.

  5. #5
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First Program - expected class-name before '{' token

    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++.

  6. #6
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: First Program - expected class-name before '{' token

    Basic usage:
    main window examples

    (...) 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):
    Qt Code:
    1. #ifndef _WIDGET_N_H__
    2. #define _WIDGET_N_H__
    3.  
    4. #include <QWidget>
    5. #include "ui_form_n.ui"
    6.  
    7. class Widget_n : public QWidget{
    8. Q_OBJECT
    9. public:
    10. Widget_n( QWidget * parent = NULL ) : QWidget(parent){
    11. _ui.setupUi(this);
    12. }
    13. protected:
    14. Ui::WidgetForm_n _ui;
    15. };
    16.  
    17. #endif
    To copy to clipboard, switch view to plain text mode 
    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.

  7. #7
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First Program - expected class-name before '{' token

    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
    Qt Code:
    1. #ifndef SIGNON_H
    2. #define SIGNON_H
    3.  
    4. #include <QWidget>
    5. #include "ui_signon.h"
    6.  
    7. class Signon : public QWidget{
    8. Q_OBJECT
    9. public:
    10. Signon( QWidget * parent = NULL ) : QWidget(parent){
    11. _ui.setupUi(this);
    12. }
    13. protected:
    14. // What does here?
    15. // Ui::MainWindow _ui;
    16. };
    17.  
    18. #endif
    To copy to clipboard, switch view to plain text mode 

    signon.cpp:
    Qt Code:
    1. #include <QtGui>
    2. #include "signon.h"
    3.  
    4. Signon::Signon(QWidget *parent)
    5. : QDialog(parent)
    6. {
    7. setupUi(this);
    8. }
    To copy to clipboard, switch view to plain text mode 

    and mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "signon.h"
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. ui->tableWidget->hide();
    12. ui::setCentralWidget( new Signon() );
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    To copy to clipboard, switch view to plain text mode 

    What do I put in for
    Ui::WidgetForm_n _ui;
    in signon.h?

    What am I doing wrong?
    Last edited by tnowacoski; 30th January 2011 at 03:27.

  8. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: First Program - expected class-name before '{' token

    Qt Code:
    1. // What does here?
    2. // Ui::MainWindow _ui;
    To copy to clipboard, switch view to plain text mode 
    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
    Qt Code:
    1. Signon( QWidget * parent = NULL ) : QWidget(parent){
    2. _ui.setupUi(this);
    3. }
    To copy to clipboard, switch view to plain text mode 
    or this:
    .cpp
    Qt Code:
    1. Signon::Signon(QWidget *parent)
    2. : QDialog(parent)
    3. {
    4. setupUi(this); //!< should be '_ui.setupUi(this)'
    5. }
    To copy to clipboard, switch view to plain text mode 

    And one more:
    Qt Code:
    1. ui::setCentralWidget( new Signon() ); //!< should be this->setCentralW...
    To copy to clipboard, switch view to plain text mode 

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

  9. #9
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: First Program - expected class-name before '{' token

    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.

    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

  10. #10
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First Program - expected class-name before '{' token

    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:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "signon.h"
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. ui->tableWidget->hide();
    10. openSignon();
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18.  
    19. void MainWindow::openSignon(){
    20. Signon signon(this);
    21. signon.exec();
    22. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by tnowacoski; 31st January 2011 at 17:53.

  11. #11
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: First Program - expected class-name before '{' token

    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

  12. #12
    Join Date
    Jan 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: First Program - expected class-name before '{' token

    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!
    Last edited by tnowacoski; 1st February 2011 at 14:58.

Similar Threads

  1. QThread expected class name before { token
    By naturalpsychic in forum Qt Programming
    Replies: 0
    Last Post: 24th January 2011, 03:42
  2. Expected class-name before '{' token
    By imagiro1 in forum Newbie
    Replies: 16
    Last Post: 16th June 2009, 11:37
  3. error: expected class-name before '{' token
    By Aresti in forum Qt Programming
    Replies: 1
    Last Post: 12th November 2008, 20:00
  4. Replies: 3
    Last Post: 10th November 2008, 16:14
  5. Replies: 2
    Last Post: 30th January 2008, 20:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.