View Full Version : why does QTableView expand to cover the buttons

5th September 2008, 12: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


#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

querymodel(QWidget *parent = 0);
public slots:
void showTable();
Ui::querymodelClass ui;
QSqlTableModel *model;

#endif // QUERYMODEL_H

// querymodel.cpp
#include "querymodel.h"

querymodel::querymodel(QWidget *parent)
: QWidget(parent)
connect( ui.pushButtonShowTable, SIGNAL(clicked()), this, SLOT(showTable()) );

delete model;

void querymodel::showTable()
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
bool ok = db.open();
model = new QSqlTableModel();
model->setTable( "log_book" );

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 );


6th September 2008, 00:48
The problem is in the .ui file, so the code you have posted is completely irrelevant. Do you have a top-level layout for all container widgets?

6th September 2008, 03:25
here is querymodel.ui

2 buttons and a table view

<ui version="4.0" >
<widget class="QWidget" name="querymodelClass" >
<property name="geometry" >
<property name="maximumSize" >
<property name="windowTitle" >
<widget class="QPushButton" name="pushButtonClose" >
<property name="geometry" >
<property name="text" >
<widget class="QPushButton" name="pushButtonShowTable" >
<property name="geometry" >
<property name="text" >
<string>Show Table</string>
<widget class="QTableView" name="tableView" >
<property name="geometry" >
<property name="verticalScrollBarPolicy" >
<property name="horizontalScrollBarPolicy" >
<layoutdefault spacing="6" margin="11" />
<hint type="sourcelabel" >
<hint type="destinationlabel" >

6th September 2008, 15:22
Well, you don't have any layouts in this .ui file. To add a top-level layout you have to click on the form's background and then click on of the "Lay out in ..." buttons.

7th September 2008, 00:35
I did what you requested and selected "layout in a form layout"

whatever that is!

now I get the following errors:

make debug
make -f Makefile.Debug
make[1]: Entering directory `/home/landon4/workspace/querymodel'
g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_SQL_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include/QtGui -I/usr/include/QtSql -I/usr/include/QtSql -I/usr/include -Idebug -I. -o debug/main.o main.cpp
In file included from querymodel.h:14,
from main.cpp:31:
ui_querymodel.h:20:25: error: qformlayout.h: No such file or directory
In file included from querymodel.h:14,
from main.cpp:31:
ui_querymodel.h:25: error: ISO C++ forbids declaration of ‘QFormLayout’ with no type
ui_querymodel.h:25: error: expected ‘;’ before ‘*’ token
ui_querymodel.h: In member function ‘void Ui_querymodelClass::setupUi(QWidget*)’:
ui_querymodel.h:36: error: ‘formLayout’ was not declared in this scope
ui_querymodel.h:36: error: expected type-specifier before ‘QFormLayout’
ui_querymodel.h:36: error: expected `;' before ‘QFormLayout’
make[1]: Leaving directory `/home/landon4/workspace/querymodel'
make[1]: *** [debug/main.o] Error 1
make: *** [debug] Error 2

I went back and this time selected "layout in a grid" right clicking in the background

as you specified.

this time the table did not overflow the buttons!

Previously I had first dragged a "grid layout" from the widgets on the left

and placed the table view and buttons inside the red rectangle. The buttons lay on

top the table!

I wonder what the widgets on the left (same selections as right clicking the form background) are for and how one would use those widgets

Nevertheless, I don't see how the table would have flowed out of its boundaries

This is a basic premise of the "widget drag system" of design

7th September 2008, 16:53
I wonder what the widgets on the left (same selections as right clicking the form background) are for and how one would use those widgets
These are for creating nested layouts and you can't create a top-level layout using them.