I have a MainWindow widget that has a stacked Widget. I want to create a class for each of the pages for the stacked widget to keep the code a little neater. When I build the program I get an error that I think might be resolved by the correct inheritence (I am a newbie, so I am not sure). The error is 'ui' was not declared in this scope. The error is identified in my class for one of the pages of the stacked widget. ui isn't a class so I am not sure how to reference it. It looks like it is a pointer to the class MainWindow which inherits the class QMainWindow. (At least this is what I think is happening). Here is what I have so far:
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
protected:
private:
Ui::MainWindow *ui;
bool updateDb();
bool saveDataCheck();
bool m_IsDirty;
void updateData();
void writeSettings();
void readSettings() ;
private slots:
void on_txtFname_textChanged();
void on_txtLname_textChanged();
void on_txtMname_textChanged();
void on_txtAddress_textChanged();
void on_cmbName_currentIndexChanged();
};
#endif // MAINWINDOW_H
#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();
protected:
void changeEvent(QEvent *e);
void closeEvent (QCloseEvent *event);
private:
Ui::MainWindow *ui;
bool updateDb();
bool saveDataCheck();
bool m_IsDirty;
void updateData();
void writeSettings();
void readSettings() ;
private slots:
void on_txtFname_textChanged();
void on_txtLname_textChanged();
void on_txtMname_textChanged();
void on_txtAddress_textChanged();
void on_cmbName_currentIndexChanged();
};
#endif // MAINWINDOW_H
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "database.h"
#include <QtSql>
#include <QSqlError>
#include <QDebug>
#include <QtGui>
#include "namewidget.h"
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
nameWidget * name= new nameWidget;
m_IsDirty=false;
ui->setupUi(this);
ui->MainStackWidget->setCurrentIndex(0);
name->updateNameComboBox();
readSettings();
updateRiderData();
}
void MainWindow
::changeEvent(QEvent *e
) {
switch (e->type()) {
ui->retranslateUi(this);
break;
default:
break;
}
}
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "database.h"
#include <QtSql>
#include <QSqlError>
#include <QDebug>
#include <QtGui>
#include "namewidget.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
nameWidget * name= new nameWidget;
m_IsDirty=false;
ui->setupUi(this);
ui->MainStackWidget->setCurrentIndex(0);
name->updateNameComboBox();
readSettings();
updateRiderData();
}
void MainWindow::changeEvent(QEvent *e)
{
QMainWindow::changeEvent(e);
switch (e->type()) {
case QEvent::LanguageChange:
ui->retranslateUi(this);
break;
default:
break;
}
}
To copy to clipboard, switch view to plain text mode
namewidget.h
#ifndef NAMEWIDGET_H
#define NAMEWIDGET_H
class nameWidget //Inheritence here?
{
public:
nameWidget();
void updateNameComboBox();
};
#endif // RIDERWIDGET_H
#ifndef NAMEWIDGET_H
#define NAMEWIDGET_H
class nameWidget //Inheritence here?
{
public:
nameWidget();
void updateNameComboBox();
};
#endif // RIDERWIDGET_H
To copy to clipboard, switch view to plain text mode
namewidget.cpp
#include "namewidget.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include "mainwindow.h"
nameWidget::nameWidget()
{
}
void nameWidget::updateNameComboBox()
{
query.exec("SELECT id,LName, FName FROM name");
ui->cmbName->blockSignals(true); //error here- ui was not declared in this scope
ui->cmbName->clear(); // I am sure I will get the same error here
while (query.next())
{
ui->cmbName->addItem(query.value (2).toString()+ " "
+ query.value (1).toString(), query.value (0).toInt());
ui->cmbName->blockSignals(false);
}
}
#include "namewidget.h"
#include "ui_mainwindow.h"
#include <QtSql>
#include "mainwindow.h"
nameWidget::nameWidget()
{
}
void nameWidget::updateNameComboBox()
{
QSqlQuery query;
query.exec("SELECT id,LName, FName FROM name");
ui->cmbName->blockSignals(true); //error here- ui was not declared in this scope
ui->cmbName->clear(); // I am sure I will get the same error here
while (query.next())
{
ui->cmbName->addItem(query.value (2).toString()+ " "
+ query.value (1).toString(), query.value (0).toInt());
ui->cmbName->blockSignals(false);
}
}
To copy to clipboard, switch view to plain text mode
I am trying to learn but keep getting a few glitches on the way. I searched to no avail.
Thanks
Bookmarks