Class declaration error



  • I'm writting a code, where i'm using designer to design the form and i also needed to update that form dynamically....
    when i not used designer to design the form, my code was like this...

    mainwindow.h

    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QtGui/QMainWindow>
     
    class QVBoxLayout;
     
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
     
    public slots:
    void addRow( void );
     
    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
     
    private:
    QVBoxLayout* m_layout;
    };
     
    #endif // MAINWINDOW_H@
    

    mainwindow.cpp
    @
    #include "mainwindow.h"

    #include <QVBoxLayout>
    #include <QHBoxLayout>
    #include <QPushButton>
    #include <QLineEdit>
     
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent),
    m_layout( new QVBoxLayout() )
    {
    QPushButton* add = new QPushButton("Add");
    connect(add, SIGNAL(clicked()), this, SLOT(addRow()));
     
    m_layout->addWidget(add);
    m_layout->setAlignment(Qt::AlignTop);
     
    QWidget* w = new QWidget();
    w->setLayout(m_layout);
     
    this->setCentralWidget(w);
    }
     
    MainWindow::~MainWindow()
    {
     
    }
     
    void MainWindow::addRow( void )
    {
    QLineEdit* line = new QLineEdit();
    QPushButton* clear = new QPushButton("Clear");
    QPushButton* remove = new QPushButton("Remove");
     
    QHBoxLayout* layout = new QHBoxLayout();
    layout->setMargin(0);
    layout->addWidget(line);
    layout->addWidget(clear);
    layout->addWidget(remove);
     
    QWidget* w = new QWidget();
    w->setLayout(layout);
     
    connect(clear, SIGNAL(clicked()), line, SLOT(clear()));
    connect(remove, SIGNAL(clicked()), w, SLOT(deleteLater()));
     
    m_layout->addWidget(w);
    }@
    

    but inorder to include my form which is designed using Qt designer...i changed it as follows..
    MainWindow::MainWindow(QWidget parent)
    @ : QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_layout( new QVBoxLayout() )
    {
    ui->setupUi(this);
    QPushButton
    add = new QPushButton("Add");
    connect(add, SIGNAL(clicked()), this, SLOT(addRow()));

    m_layout->addWidget(add);
    .............
    .............@
    

    But here i'm getting error saying..
    error: class 'MainWindow' does not have any field named 'ui'
    error: invalid use of incomplete type 'struct Ui::MainWindow'
    error: forward declaration of 'struct Ui::MainWindow'
    error: 'ui' was not declared in this scope

    please help me wher i'm going wrong..!!!?

    Moderator note: please don't shout. Using all caps and three exclamation marks is considered shouting, and thus rude. Also, the Qt Quick forum is not for quick answers, but about the QML/Quick technology, so I moved your topic. Andre



  • Here is the default defenition of MainWindow with form.
    Header:
    @

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    private:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H
    @
    Source:
    @

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

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

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @
    So look what you miss and try to correct.



  • i did,,,,but i'm getting error saying that
    error: invalid use of incomplete type 'struct Ui::MainWindow'

    at line... " ui(new Ui::MainWindow)" in the code
    @MainWindow::MainWindow(QWidget parent)
    : QMainWindow(parent),
    ui(new Ui::MainWindow),
    m_layout( new QVBoxLayout() )
    {
    ui->setupUi(this);
    QPushButton
    add = new QPushButton("Add");
    connect(add, SIGNAL(clicked()), this, SLOT(addRow()));@

    please let me know whats wrong?



  • Have you add this @#include "ui_mainwindow.h"@ ?



  • Having an "undefined <something>" error almost always is the result of one of these errors:

    • A typo in the type name
    • A forgotten #include

    There are some other possibilities, but I won't bore you with those.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.