thefatladysingsopera
27th July 2011, 12:23
I have made an application and it has a Qtableview,four form fields of Qlinedit,Qgridlayout and five buttons.The application builds without any errors but exits when i try to run it with the windows error message : "smith.exe has encountered a problem and needs to close. We are sorry for the inconvenience.".
Other examples i have run correctly.
Here is the code.
smith.h
#ifndef SMITH_H
#define SMITH_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QPushButton;
class QLabel;
class QLineEdit;
class QTableView;
class QDataWidgetMapper;
class QSqlTableModel;
QT_END_NAMESPACE
class smith : public QWidget
{
Q_OBJECT
public:
smith(QWidget *parent = 0);
private:
void setupModel();
QDataWidgetMapper *mapper;
QSqlTableModel *tableModel;
QTableView *tableView;
QLabel *fnLabel;
QLabel *lnLabel;
QLabel *countryLabel;
QLabel *cityLabel;
QLineEdit *fEdit;
QLineEdit *lnEdit;
QLineEdit *cEdit;
QLineEdit *cityEdit;
QPushButton *newButton;
QPushButton *nextButton;
QPushButton *previousButton;
QPushButton *saveButton;
QPushButton *deleteButton;
private slots:
void on_deleteButton_clicked();
void on_saveButton_clicked();
void on_newButton_clicked();
};
#endif // SMITH_H
smith.cpp
#include <QtGui>
#include <QtSql>
#include "smith.h"
smith::smith(QWidget *parent) :
QWidget(parent)
{
setupModel();
QLabel *fnLabel = new QLabel(tr("First Name:"));
fEdit = new QLineEdit;
fEdit->setReadOnly(true);
QLabel *lnLabel = new QLabel(tr("Last Name:"));
lnEdit = new QLineEdit;
lnEdit->setReadOnly(true);
QLabel *countryLabel = new QLabel(tr("Country:"));
cEdit = new QLineEdit;
cEdit->setReadOnly(true);
QLabel *cityLabel = new QLabel(tr("City:"));
cityEdit = new QLineEdit;
cityEdit->setReadOnly(true);
newButton = new QPushButton(tr("&New"));
nextButton = new QPushButton(tr("&Next"));
previousButton = new QPushButton(tr("&Previous"));
saveButton = new QPushButton(tr("&Save"));
deleteButton = new QPushButton(tr("&Delete"));
connect(newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(tableView);
mainLayout->addWidget(fnLabel);
mainLayout->addWidget(fEdit);
mainLayout->addWidget(lnLabel);
mainLayout->addWidget(lnEdit);
mainLayout->addWidget(countryLabel);
mainLayout->addWidget(cEdit);
mainLayout->addWidget(cityLabel);
mainLayout->addWidget(cityEdit);
mainLayout->addWidget(newButton);
mainLayout->addWidget(nextButton);
mainLayout->addWidget(previousButton);
mainLayout->addWidget(saveButton);
mainLayout->addWidget(deleteButton);
setLayout(mainLayout);
setWindowTitle(tr("Application Layout"));
//Model setup
tableModel = new QSqlTableModel(this);
tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
tableModel->setTable("application");
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(1, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("City"));
fnLabel->setBuddy(fEdit);
lnLabel->setBuddy(lnEdit);
countryLabel->setBuddy(cEdit);
cityLabel->setBuddy(cityEdit);
//Mapper
mapper = new QDataWidgetMapper(this);
mapper->setModel(tableModel);
mapper->addMapping(fEdit, 0);
mapper->addMapping(lnEdit, 1);
mapper->addMapping(cEdit, 2);
mapper->addMapping(cityEdit, 3);
mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
mapper->toFirst();
//TableView
tableView = new QTableView(this);
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
tableView->setWindowTitle("Application Data");
}
void smith::setupModel()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("smith.DAT");
if (!db.open()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}
}
void smith::on_newButton_clicked()
{
}
void smith::on_saveButton_clicked()
{
}
void smith::on_deleteButton_clicked()
{
}
main.cpp
#include <QtGui/QApplication>
#include "smith.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
smith smith;
smith.show();
return a.exec();
}
How do i get the qtableview,the buttons and the forms to display correctly?.
Other examples i have run correctly.
Here is the code.
smith.h
#ifndef SMITH_H
#define SMITH_H
#include <QWidget>
QT_BEGIN_NAMESPACE
class QPushButton;
class QLabel;
class QLineEdit;
class QTableView;
class QDataWidgetMapper;
class QSqlTableModel;
QT_END_NAMESPACE
class smith : public QWidget
{
Q_OBJECT
public:
smith(QWidget *parent = 0);
private:
void setupModel();
QDataWidgetMapper *mapper;
QSqlTableModel *tableModel;
QTableView *tableView;
QLabel *fnLabel;
QLabel *lnLabel;
QLabel *countryLabel;
QLabel *cityLabel;
QLineEdit *fEdit;
QLineEdit *lnEdit;
QLineEdit *cEdit;
QLineEdit *cityEdit;
QPushButton *newButton;
QPushButton *nextButton;
QPushButton *previousButton;
QPushButton *saveButton;
QPushButton *deleteButton;
private slots:
void on_deleteButton_clicked();
void on_saveButton_clicked();
void on_newButton_clicked();
};
#endif // SMITH_H
smith.cpp
#include <QtGui>
#include <QtSql>
#include "smith.h"
smith::smith(QWidget *parent) :
QWidget(parent)
{
setupModel();
QLabel *fnLabel = new QLabel(tr("First Name:"));
fEdit = new QLineEdit;
fEdit->setReadOnly(true);
QLabel *lnLabel = new QLabel(tr("Last Name:"));
lnEdit = new QLineEdit;
lnEdit->setReadOnly(true);
QLabel *countryLabel = new QLabel(tr("Country:"));
cEdit = new QLineEdit;
cEdit->setReadOnly(true);
QLabel *cityLabel = new QLabel(tr("City:"));
cityEdit = new QLineEdit;
cityEdit->setReadOnly(true);
newButton = new QPushButton(tr("&New"));
nextButton = new QPushButton(tr("&Next"));
previousButton = new QPushButton(tr("&Previous"));
saveButton = new QPushButton(tr("&Save"));
deleteButton = new QPushButton(tr("&Delete"));
connect(newButton, SIGNAL(clicked()), this, SLOT(on_newButton_clicked()));
connect(nextButton, SIGNAL(clicked()), mapper, SLOT(toNext()));
connect(previousButton, SIGNAL(clicked()), mapper, SLOT(toPrevious()));
connect(saveButton, SIGNAL(clicked()), this, SLOT(on_saveButton_clicked()));
connect(deleteButton, SIGNAL(clicked()), this, SLOT(on_deleteButton_clicked()));
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(tableView);
mainLayout->addWidget(fnLabel);
mainLayout->addWidget(fEdit);
mainLayout->addWidget(lnLabel);
mainLayout->addWidget(lnEdit);
mainLayout->addWidget(countryLabel);
mainLayout->addWidget(cEdit);
mainLayout->addWidget(cityLabel);
mainLayout->addWidget(cityEdit);
mainLayout->addWidget(newButton);
mainLayout->addWidget(nextButton);
mainLayout->addWidget(previousButton);
mainLayout->addWidget(saveButton);
mainLayout->addWidget(deleteButton);
setLayout(mainLayout);
setWindowTitle(tr("Application Layout"));
//Model setup
tableModel = new QSqlTableModel(this);
tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
tableModel->setTable("application");
tableModel->select();
tableModel->setHeaderData(0, Qt::Horizontal, tr("First Name"));
tableModel->setHeaderData(1, Qt::Horizontal, tr("Last Name"));
tableModel->setHeaderData(2, Qt::Horizontal, tr("Country"));
tableModel->setHeaderData(3, Qt::Horizontal, tr("City"));
fnLabel->setBuddy(fEdit);
lnLabel->setBuddy(lnEdit);
countryLabel->setBuddy(cEdit);
cityLabel->setBuddy(cityEdit);
//Mapper
mapper = new QDataWidgetMapper(this);
mapper->setModel(tableModel);
mapper->addMapping(fEdit, 0);
mapper->addMapping(lnEdit, 1);
mapper->addMapping(cEdit, 2);
mapper->addMapping(cityEdit, 3);
mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
mapper->toFirst();
//TableView
tableView = new QTableView(this);
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
tableView->setWindowTitle("Application Data");
}
void smith::setupModel()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("smith.DAT");
if (!db.open()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}
}
void smith::on_newButton_clicked()
{
}
void smith::on_saveButton_clicked()
{
}
void smith::on_deleteButton_clicked()
{
}
main.cpp
#include <QtGui/QApplication>
#include "smith.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
smith smith;
smith.show();
return a.exec();
}
How do i get the qtableview,the buttons and the forms to display correctly?.