Results 1 to 5 of 5

Thread: String length limited in QTableWidgetItem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.)

  2. #2
    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 

  3. #3
    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 

  4. #4
    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.