landonmkelsey
5th September 2008, 11:53
I did this program outside Eclipse(with built in Qt4)
and inside Eclipse.
Outside Eclipse, the table view is its own "dialog" OK!
Scroll bars show up perfectly!
Inside Eclipse the tableview shares a dialog with two buttons:
(1) show table(from MySQL database)
(2) close
When I hit the button "show table" the table shows in the table view but covers the two buttons.
Using grid layout, the buttons are on top of the table view.
Scroll bars work! I tried vertical and horizontal layout even with spacers!
I inserted the table view into a frame and the problem is solved except that there are no scrollbars!
I did try using the qt designer OUTSIDE Eclipse!
Why does the table view flood the dialog?
// querymodel.h
#ifndef QUERYMODEL_H
#define QUERYMODEL_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QtDebug>
#include <QtGui/QWidget>
#include "ui_querymodel.h"
class querymodel : public QWidget
{
Q_OBJECT
public:
querymodel(QWidget *parent = 0);
~querymodel();
public slots:
void showTable();
private:
Ui::querymodelClass ui;
QSqlTableModel *model;
};
#endif // QUERYMODEL_H
// querymodel.cpp
#include "querymodel.h"
querymodel::querymodel(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect( ui.pushButtonShowTable, SIGNAL(clicked()), this, SLOT(showTable()) );
}
querymodel::~querymodel()
{
delete model;
}
void querymodel::showTable()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("3141");
db.setConnectOptions(QString("CLIENT_INTERACTIVE"));
bool ok = db.open();
model = new QSqlTableModel();
model->setTable( "log_book" );
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
model->setHeaderData( 0, Qt::Horizontal, QObject::tr("rowid") );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr("fdate") );
model->setHeaderData( 2, Qt::Horizontal, QObject::tr("acid") );
model->setHeaderData( 3, Qt::Horizontal, QObject::tr("actype") );
model->setHeaderData( 4, Qt::Horizontal, QObject::tr("nlandings") );
model->setHeaderData( 5, Qt::Horizontal, QObject::tr("nhours") );
// QTableView *view = new QTableView();
ui.tableView->setModel( model );
ui.tableView->setAlternatingRowColors ( true );
ui.tableView->setFixedSize ( 800,500 );
ui.tableView->show();
}
and inside Eclipse.
Outside Eclipse, the table view is its own "dialog" OK!
Scroll bars show up perfectly!
Inside Eclipse the tableview shares a dialog with two buttons:
(1) show table(from MySQL database)
(2) close
When I hit the button "show table" the table shows in the table view but covers the two buttons.
Using grid layout, the buttons are on top of the table view.
Scroll bars work! I tried vertical and horizontal layout even with spacers!
I inserted the table view into a frame and the problem is solved except that there are no scrollbars!
I did try using the qt designer OUTSIDE Eclipse!
Why does the table view flood the dialog?
// querymodel.h
#ifndef QUERYMODEL_H
#define QUERYMODEL_H
#include <QMessageBox>
#include <QSqlDatabase>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QtDebug>
#include <QtGui/QWidget>
#include "ui_querymodel.h"
class querymodel : public QWidget
{
Q_OBJECT
public:
querymodel(QWidget *parent = 0);
~querymodel();
public slots:
void showTable();
private:
Ui::querymodelClass ui;
QSqlTableModel *model;
};
#endif // QUERYMODEL_H
// querymodel.cpp
#include "querymodel.h"
querymodel::querymodel(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
connect( ui.pushButtonShowTable, SIGNAL(clicked()), this, SLOT(showTable()) );
}
querymodel::~querymodel()
{
delete model;
}
void querymodel::showTable()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("localhost");
db.setDatabaseName("landonx");
db.setUserName("landon4");
db.setPassword("3141");
db.setConnectOptions(QString("CLIENT_INTERACTIVE"));
bool ok = db.open();
model = new QSqlTableModel();
model->setTable( "log_book" );
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();
model->setHeaderData( 0, Qt::Horizontal, QObject::tr("rowid") );
model->setHeaderData( 1, Qt::Horizontal, QObject::tr("fdate") );
model->setHeaderData( 2, Qt::Horizontal, QObject::tr("acid") );
model->setHeaderData( 3, Qt::Horizontal, QObject::tr("actype") );
model->setHeaderData( 4, Qt::Horizontal, QObject::tr("nlandings") );
model->setHeaderData( 5, Qt::Horizontal, QObject::tr("nhours") );
// QTableView *view = new QTableView();
ui.tableView->setModel( model );
ui.tableView->setAlternatingRowColors ( true );
ui.tableView->setFixedSize ( 800,500 );
ui.tableView->show();
}