You are not logged in.

chang112x

Beginner

  • "chang112x" is male
  • "chang112x" started this thread

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

1

Wednesday, June 8th 2011, 2:03am

QTableView doesn't paint correctly

Hi, i'm programming a simple sqlite manager in qt.
All is working, just the fact that the QTableView i use, doesn't paint correctly where i positioned it in the designer, instead, it obscure all the menu, and i can't access it, and this is not good, obviously.
Data is loaded correctly when i open the database file.
I attach i file to let you see what is the situation.

How i can fix it ?

I also post the source code:

The Constructor

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
FiereMainWindow::FiereMainWindow(QWidget *parent) :     QMainWindow(parent),
     ui(new Ui::FiereMainWindow),
     defaultDatabaseName("fiere.db") {
      ui->setupUi(this);
      sqliteDatabase = QSqlDatabase::addDatabase("QSQLITE");
      sqlTableModel = new QSqlTableModel(this, sqliteDatabase);
     sqlTableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
      databaseTableView = new QTableView(this);
     databaseTableView->setModel(sqlTableModel);
     databaseTableView->setFixedHeight(500);
     databaseTableView->setFixedWidth(500);
      if(!openSqliteDatabase((QString&)defaultDatabaseName)) {
        statusBar()->showMessage(tr("File Database non aperto, errore."));
      }
  }


The open function

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
bool FiereMainWindow::openSqliteDatabase(QString& path) {      const char* databaseTableUtenteFields[] = {
          "id"       ,
         "nome"     ,
         "cognome"  ,
         "nazione"  ,
         "fiera_id" ,
         "anno"     ,
         "indirizzo",
         "citta"    ,
         "code"     ,
         "email"    ,
         "importo"
      };
     QString prova;
     QString fileNotFound_ErrorMessage = tr("Impossibile aprire il file ") + path + tr(": il file non esiste o c'è stato qualche altro problema. Verificare la presenza del file nel sistema, il path, la cartella, la configurazione e quanto può essere possibile per risolvere il problema");
     if(!QFile::exists(path)) {
        QMessageBox::warning(this, tr("Warning"), fileNotFound_ErrorMessage, QMessageBox::Ok);
       return false;
     }
      sqliteDatabase.setDatabaseName(path);
     if(!sqliteDatabase.open()) {
         QMessageBox::warning(this, tr("Warning"), tr("Errore durante l'apertura del database") + sqliteDatabase.lastError().databaseText(), QMessageBox::Ok);
        return false;
     }
     sqlTableModel->setTable("UTENTE");
     for(unsigned int i = 0; i < sizeof(databaseTableUtenteFields) / sizeof(databaseTableUtenteFields[0]); i++) {
        sqlTableModel->setHeaderData(i, Qt::Horizontal, tr(databaseTableUtenteFields[i]));
        prova += databaseTableUtenteFields[i];
       prova += "\n";
     }
     qDebug(qPrintable(prova));
     sqlTableModel->select();
    sqlTableModel->submitAll();
     statusBar()->showMessage(tr("File Database aperto, all is good."));
     return true;
  }


And that's it.
If you need something else, just ask.
I'm running QtCreator 2.1.0, with Qt 4.7.1, on Windows 7 Home Premium machine.

Thank you in advance,
Marco.
chang112x has attached the following files:

pele1410

Trainee

  • "pele1410" is male

Posts: 127

Location: Lafayette, LA

  • Send private message

2

Wednesday, June 8th 2011, 1:04pm

If you've created databaseTableView in your UI file, then you don't need to create another one in the class files. Just reference the UI::databaseTableView. You're basically creating 2 instances of the tableView right now. This should work:

.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <QMainWindow>
#include "ui_FiereMainWindow.h"

class FiereMainWindow: public QMainWindow, private Ui::FiereMainWindow
{
   Q_OBJECT

public:
   FiereMainWindow(QWidget *parent = 0);

private:
   Ui::FiereMainWindow ui;
};


//.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FiereMainWindow::FiereMainWindow(QWidget *parent) : QMainWindow(parent),
     defaultDatabaseName("fiere.db") 
{
   ui.setupUi(this);

   sqliteDatabase = QSqlDatabase::addDatabase("QSQLITE");
   sqlTableModel = new QSqlTableModel(this, sqliteDatabase);
   sqlTableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
     
   ui.databaseTableView->setModel(sqlTableModel);
   ui.databaseTableView->setFixedHeight(500);
   ui.databaseTableView->setFixedWidth(500);
   if(!openSqliteDatabase((QString&)defaultDatabaseName)) {
      statusBar()->showMessage(tr("File Database non aperto, errore."));
   }
}
"Without imperfection, you or I would not exist." ~Stephen Hawking

chang112x

Beginner

  • "chang112x" is male
  • "chang112x" started this thread

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

3

Thursday, June 9th 2011, 7:07am

Ok Thank you very much ! Now is working quite perfectly !
One thing, how i can make the tableview area to expand to fill all the coloums, and scrollable ?

Thank you !

chang112x

Beginner

  • "chang112x" is male
  • "chang112x" started this thread

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

4

Thursday, June 9th 2011, 7:31am

Now is working in that sense, just a last question.
How i can make a layout expand within the window ?

chang112x

Beginner

  • "chang112x" is male
  • "chang112x" started this thread

Posts: 15

Occupation: Programmer and all sort of thing to survive.

  • Send private message

5

Thursday, June 9th 2011, 7:33am

Ok now also that is fixed.

Thank you ! :)

Goodbye,
Marco.