Results 1 to 5 of 5

Thread: String length limited in QTableWidgetItem

  1. #1
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post String length limited in QTableWidgetItem

    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.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: String length limited in QTableWidgetItem

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

  3. #3
    Join Date
    Nov 2010
    Posts
    14
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Thumbs up Re: String length limited in QTableWidgetItem

    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
    Qt Code:
    1. #ifndef STRINGDELEGATE_H
    2. #define STRINGDELEGATE_H
    3.  
    4. #include <QItemDelegate>
    5. #include <QLineEdit>
    6. class stringDelegate : public QItemDelegate
    7. {
    8. Q_OBJECT
    9. public:
    10. explicit stringDelegate(int length, QObject *parent = 0);
    11. QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,const QModelIndex &index) const;
    12.  
    13. signals:
    14.  
    15. public slots:
    16. void commitAndCloseEditor();
    17.  
    18. private:
    19. int length;
    20. };
    21.  
    22. #endif // STRINGDELEGATE_H
    To copy to clipboard, switch view to plain text mode 


    *.cpp
    Qt Code:
    1. #include "stringdelegate.h"
    2.  
    3. stringDelegate::stringDelegate(int length, QObject *parent) :
    4. QItemDelegate(parent)
    5. {
    6. this->length=length;
    7. }
    8.  
    9. QWidget* stringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/*option*/,const QModelIndex &/*index*/ ) const{
    10. QLineEdit *line = new QLineEdit(parent);
    11. line->setMaxLength(length);
    12. line->setFrame(false);
    13. connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
    14. return line;
    15. }
    16.  
    17. void stringDelegate::commitAndCloseEditor(){
    18. QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
    19. emit commitData(editor);
    20. emit closeEditor(editor);
    21. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Aug 2009
    Location
    Brisbane, Australia
    Posts
    75
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: String length limited in QTableWidgetItem

    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

    Qt Code:
    1. line->setFrame(false);
    2. connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
    3. ...
    4. ...
    5. ...
    6. void stringDelegate::commitAndCloseEditor(){
    7. QLineEdit *editor = qobject_cast<QLineEdit *>(sender());
    8. emit commitData(editor);
    9. emit closeEditor(editor);
    10. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Sep 2013
    Posts
    40
    Thanks
    6
    Thanked 3 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: String length limited in QTableWidgetItem

    why is it giving the error --> reference to 'connect' is ambiguous?

Similar Threads

  1. Does QSqlTableModel have limited capacity?
    By metdos in forum Qt Programming
    Replies: 2
    Last Post: 1st January 2010, 22:06
  2. Replies: 1
    Last Post: 22nd August 2008, 09:12
  3. saving a c string of variable length in a shared memory?
    By nass in forum General Programming
    Replies: 4
    Last Post: 3rd January 2007, 14:40
  4. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55
  5. Why are actions limited to menus and toolbars?
    By Paul Drummond in forum Qt Programming
    Replies: 16
    Last Post: 3rd March 2006, 12:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.