PDA

View Full Version : String length limited in QTableWidgetItem



joseprl89
24th November 2010, 09:07
Hi, it's my first post here, but it's not the first time i visited this site in order to find some answers to my problems. This time, however, i haven't seen any post related to what i'm trying to do, so here it goes.

I'm working in a ERP Qt application, in which i use a MYSQL database, and i need to show the data in a table, even i know i can use a QTableModel for accessing the database, i prefer to do the access to the DB on my own, because it's an application in which i would like the maximum control of it, so i used a QTableWidget, and populate it with the data i receive from the DB.

However, the user must be able to insert or modify this data, so i have to set a limit to the strings displayed in the QTableWidget, so they don't insert a longer string, which the DB would not accept.

I've tried to subclass QTableWidget and reimplement the keyPressEvent so it stops the text from exceeding the maximum length, also i've tried to use QLineEdit's as a cellWidget, and limit the length of the lineedits, but it don't like it's behaviour, because it gets the application focus in a strange manner.

I would like to know, which class has the keyPressEvent that "sends" the text into the QTableWidgetItem, because it appears in the Qt Manual that the QTableWidgetItem is not a subclass of QObject nor QWidget and also, it doesn't use the keyPressEvent.

Any help would be very appreciated.

P.S: Sorry for my english, I'm spanish.

Lykurg
24th November 2010, 10:10
Internal QTableWidget uses a model, so you could consider making your own model so that you can keep control. But to your problem: One way I think of right now is to set a custom delegate via QAbstractItemView::setItemDelegate(). In there you define the editor widget (which defines the limit of characters). This way the focus wont be a problem.
Or you can also have a look at QItemEditorFactory (but I have never worked with that, so I don't exactly know if it could fit your needs.)

joseprl89
24th November 2010, 12:24
Thanks for your advice, now it works perfectly without strange behaviours, if someone has the same problem, here comes the code of the delegate:

*.h


#ifndef STRINGDELEGATE_H
#define STRINGDELEGATE_H

#include <QItemDelegate>
#include <QLineEdit>
class stringDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit stringDelegate(int length, QObject *parent = 0);
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;

signals:

public slots:
void commitAndCloseEditor();

private:
int length;
};

#endif // STRINGDELEGATE_H



*.cpp


#include "stringdelegate.h"

stringDelegate::stringDelegate(int length, QObject *parent) :
QItemDelegate(parent)
{
this->length=length;
}

QWidget* stringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/ ) const{
QLineEdit *line = new QLineEdit(parent);
line->setMaxLength(length);
line->setFrame(false);
connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
return line;
}

void stringDelegate::commitAndCloseEditor(){
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}

grantbj74
18th October 2011, 06:05
Hi,

I have seen the following code in a few examples. Is it needed ? My code seems to work without it.

Thanks in advance, Brendan



line->setFrame(false);
connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
...
...
...
void stringDelegate::commitAndCloseEditor(){
QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
emit commitData(editor);
emit closeEditor(editor);
}

rohitkk
25th September 2013, 12:34
why is it giving the error --> reference to 'connect' is ambiguous?