PDA

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



landonmkelsey
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

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


}

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

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

2 buttons and a table view



<ui version="4.0" >
<class>querymodelClass</class>
<widget class="QWidget" name="querymodelClass" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>698</width>
<height>561</height>
</rect>
</property>
<property name="maximumSize" >
<size>
<width>16776957</width>
<height>16777215</height>
</size>
</property>
<property name="windowTitle" >
<string>querymodel</string>
</property>
<widget class="QPushButton" name="pushButtonClose" >
<property name="geometry" >
<rect>
<x>10</x>
<y>450</y>
<width>83</width>
<height>30</height>
</rect>
</property>
<property name="text" >
<string>Close</string>
</property>
</widget>
<widget class="QPushButton" name="pushButtonShowTable" >
<property name="geometry" >
<rect>
<x>10</x>
<y>410</y>
<width>83</width>
<height>30</height>
</rect>
</property>
<property name="text" >
<string>Show Table</string>
</property>
</widget>
<widget class="QTableView" name="tableView" >
<property name="geometry" >
<rect>
<x>10</x>
<y>20</y>
<width>271</width>
<height>201</height>
</rect>
</property>
<property name="verticalScrollBarPolicy" >
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy" >
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
</widget>
</widget>
<layoutdefault spacing="6" margin="11" />
<resources/>
<connections>
<connection>
<sender>pushButtonClose</sender>
<signal>clicked()</signal>
<receiver>querymodelClass</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel" >
<x>391</x>
<y>274</y>
</hint>
<hint type="destinationlabel" >
<x>348</x>
<y>227</y>
</hint>
</hints>
</connection>
</connections>
</ui>

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

landonmkelsey
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

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