PDA

View Full Version : [QT4 & XP] QTreeView issue with Designer form



incapacitant
2nd March 2006, 17:34
I have this problem with the code below, that when I double click on an row, then nothing happens. I have written about the same code without a designer form and it works. I must be doing several things wrong in the designer form reuse.

Can you point out the where the trouble(s) is(are) :

main.cpp


#include <QtCore>
#include <QtGui>

#include "dialog.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
Dialog *window = new Dialog;
window->show();
return app.exec();
}


dialog.h


#include "ui_dialog.h"
#include <QStandardItemModel>

class Dialog : public QDialog
{
Q_OBJECT

public:
Dialog(QWidget *parent = 0);

private:
Ui::Dialog ui;
QStandardItemModel *model;

protected slots:
void selection(const QModelIndex&);
};


dialog.cpp


#include <QtCore>
#include <QtGui>

#include "ui_dialog.h"
#include "dialog.h"

class QAbstractItemModel;
class QAbstractItemView;
class QItemSelectionModel;
class QStandardItemModel;

Dialog::Dialog(QWidget *parent) : QDialog(parent)
{
ui.setupUi(this);
QStandardItemModel *model = new QStandardItemModel(0, 2, this);
model->setHeaderData(0, Qt::Horizontal, "Mois");
model->setHeaderData(1, Qt::Horizontal, "Facture");

QPalette palette;
QPixmap pixmap = QPixmap("./images/bBackground.png");
palette.setBrush((this)->backgroundRole(), QBrush(pixmap));
(this)->setPalette(palette);

ui.treeView->setModel(model);
QItemSelectionModel *selectionModel = new QItemSelectionModel(model);
ui.treeView->setSelectionModel(selectionModel);

model->removeRows(0, model->rowCount(QModelIndex()), QModelIndex());

QString month = "October";
QString amount = "5 Euros";
int row = model->rowCount(QModelIndex());
model->insertRows(row, 1, QModelIndex());
model->setData(model->index(row, 0, QModelIndex()), month );
model->setData(model->index(row, 1, QModelIndex()), amount );

month = "November";
amount = "10 Euros";
row = model->rowCount(QModelIndex());
model->insertRows(row, 1, QModelIndex());
model->setData(model->index(row, 0, QModelIndex()), month );
model->setData(model->index(row, 1, QModelIndex()), amount );

ui.treeView->setEditTriggers(QAbstractItemView::NoEditTriggers) ;
ui.treeView->setRootIsDecorated( false );
// works perfect up to here, does not seem to connect then...:o
connect( ui.treeView, SIGNAL( doubleClicked(const ui.QModelIndex&) ),
this, SLOT( selection(const ui.QModelIndex&) ) );
}

void Dialog::selection(const QModelIndex& idx)
{
QString month = "Selection added";
QString amount = "below:";
int row;
row = model->rowCount(QModelIndex());
model->insertRows(row, 1, QModelIndex());
model->setData(model->index(row, 0, QModelIndex()), month );
model->setData(model->index(row, 1, QModelIndex()), amount );

QString col0 = model->data(idx.sibling(idx.row(), 0)).toString();
QString col1 = model->data(idx.sibling(idx.row(), 1)).toString();
row = model->rowCount(QModelIndex());
model->insertRows(row, 1, QModelIndex());
model->setData(model->index(row, 0, QModelIndex()), col0 );
model->setData(model->index(row, 1, QModelIndex()), col1 );
}


ui_dialog.h // generated by Qt


#ifndef UI_DIALOG_H
#define UI_DIALOG_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QDialog>
#include <QtGui/QTreeView>

class Ui_Dialog
{
public:
QTreeView *treeView;

void setupUi(QDialog *Dialog)
{
Dialog->setObjectName(QString::fromUtf8("Dialog"));
Dialog->resize(QSize(400, 230).expandedTo(Dialog->minimumSizeHint()));
treeView = new QTreeView(Dialog);
treeView->setObjectName(QString::fromUtf8("treeView"));
treeView->setGeometry(QRect(89, 60, 201, 80));
retranslateUi(Dialog);

QMetaObject::connectSlotsByName(Dialog);
} // setupUi

void retranslateUi(QDialog *Dialog)
{
Dialog->setWindowTitle(QApplication::translate("Dialog", "Dialog", 0, QApplication::UnicodeUTF8));
Q_UNUSED(Dialog);
} // retranslateUi

};

namespace Ui {
class Dialog: public Ui_Dialog {};
} // namespace Ui

wysota
2nd March 2006, 17:39
Did you have a look at the console? I'm sure it'll tell you "there is no such signal".


connect( ui.treeView, SIGNAL( doubleClicked(const ui.QModelIndex&) ),
this, SLOT( selection(const ui.QModelIndex&) ) );

Change it to:


connect( ui.treeView, SIGNAL( doubleClicked(const QModelIndex&) ),
this, SLOT( selection(const QModelIndex&) ) );

See the difference? ;)

incapacitant
2nd March 2006, 18:10
There is a difference but it is not very good. When I double click on one row, the program crashes. Proof I guess that the slot is activated, and that the problem is in the slot code.
If I remove all but the 3 variables declarations then it does not crash, but it won't increment the content of the model or the view.:o

incapacitant
2nd March 2006, 18:42
in the end i fixed it after you gave your clue. thx very much:D