PDA

View Full Version : How to display my application gui



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

ChrisW67
27th July 2011, 12:49
The crash is one of any number of things: missing DLLs and null pointer dereferencing are top of the list. Run the program in a debugger and look at the backtrace.

You should put your table view into the layout.
Your use of the grid layout is odd. Why the large column and row numbers?
Lines 11, 15, 19, 23 of the cpp file declare locals that mask member variables. Not likely to be a problem as-is, but might be later of you access, for example, the fnLabel member variable and it is not initialised.

thefatladysingsopera
27th July 2011, 13:06
You should put your table view into the layout.

How can i do that?.

norobro
27th July 2011, 13:48
Check the order of the statements in your constructor. For instance, what does mapper in line 34 point to? And tableView in line 40.

ChrisW67
27th July 2011, 23:25
Bringing a Private Message back to the thread. Please do not ask questions by PM, it hides answers from others.

the big numbers like 325,251 shouldn't they be the geometry.Secondly,how can i put my qtableview inside the layout and does that also apply for all other widgets in the application i am building i.e forms?.
No, the row and column are the indexes of cells in the grid layout. So, if you want the space occupied by the grid to be divided into 3 rows of 5 cells you use row numbers from 0 to 2 and column from 0 to 4 to place widgets in the cells. The layout worries about where these cell boundaries map to on the available screen space and sizes the contained widget accordingly. Read Layout Management and QGridLayout.

You put the table view into the layout exactly the same way you have put all the other widgets into the layout.

gkarthick5
28th July 2011, 07:22
Regarding the crashing:
Try renaming the variable smith in main.cpp to some other name. Using the class name causes crashes at times.

thefatladysingsopera
28th July 2011, 15:24
The program did run eventually.I was following the address book example before.The books application example worked for me.

Thanks.