Results 1 to 7 of 7

Thread: Is it possible to cast QTableWidgetItem ti QLineEdit

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Is it possible to cast QTableWidgetItem ti QLineEdit

    I have QTableWidget, and in the second column I want to provide to user a completer feature, how ever, to accomplish this, I attached a QLineEdit to the specified column. Now I am facing a new problem, since my QTableWidget has to be auto grown(when ever user type a character in the widget , a new row inserted to QTableWidget), so, after reading documentation, I found itemChanged(QTableWidgetItem*) SIGNAL may help me.
    SO, when ever a user type some thing in the item(say column 2, row 0), I catch the item changed, cast it to QLineEdit, set completer to it, user will be able to see suggestion, a new row inserted.

    Qt Code:
    1. connect(ui->billTableWidget, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(addRow(QTableWidgetItem*)));
    2.  
    3. Store::addRow(QTableWidgetItem*)
    4. {
    5. QLineEdit *itemEdit = new QLineEdit;
    6. itemEdit = dynamic_cast<QLineEdit*>(item);
    7. Clients client(ui);
    8. QStringList clients = client.getClients();
    9. completer = new QCompleter(clients, itemEdit);
    10. completer->setCaseSensitivity(Qt::CaseInsensitive);
    11. completer->setCompletionMode(QCompleter::PopupCompletion);
    12. itemEdit->setCompleter(completer);
    13. }
    To copy to clipboard, switch view to plain text mode 
    But this unfrountly cause the application to be exited on signal 11 (core dumped) like the console said. Is there some thing missing?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    A QTableWidgetItem is a data object not a widget. You cannot cast from one to the other. Your code crashes because itemEdit at line 6 will be NULL.

    If you need access to customise the widget used when a cell is edited you should create and attach a QStyledItemDelegate

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    Not only that, but in line 4 why do you create a new QLineEdit, assign the pointer to "itemEdit", then immediately throw it away by trying to assign another value to the pointer in line 5?

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    The whole premise of the question is a little strange anyway. There will not be an item at row 2 column 0 to edit unless there is already a row 2 in the widget's model, yet SIFE it talking about doing this as the row is added or adding a separate row as the user types. It really isn't very clear.

  5. #5
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    I am trying to avoid attaching a QLineEdit to (row = n, col = 2, n refer to the number of row inserted), since QTableWidget provide lineedit, I tried to use in order to attache it with my completer. The problem I am facing now is, if I attached a QLineEdit, I lost the signals of QTableWidget in row = n, col = 2, while other cells still can be notified if change some thing or click on theme, for this reason I am looking for a way to notifier me if the widget in row =n and col=2 changed or not.

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    Quote Originally Posted by SIFE View Post
    I am trying to avoid attaching a QLineEdit to (row = n, col = 2, n refer to the number of row inserted),
    Why?
    since QTableWidget provide lineedit, I tried to use in order to attache it with my completer.
    Use a QStyleItemDelegate subclass and create your own QLineEdit with a QCompleter attached when the cell is edited. An editing widget does not exist until the user attempts to edit the cell. The editor is created by a default delegate, used, and destroyed when the user presses Enter or leaves the cell. To intercept this and substitute your enhanced editor you need to replace the delegate. The data item is accessed through the underlying model by the delegate to populate the editor and to write the edited value back at the completion of editing.
    The problem I am facing now is, if I attached a QLineEdit, I lost the signals of QTableWidget in row = n, col = 2, while other cells still can be notified if change some thing or click on theme,
    The problem you are facing with the code above is that you are doing nothing like attaching a completer to the default line edit... your code will only crash the program. The data item associated with a table cell is not a widget and you cannot treat it as one.
    for this reason I am looking for a way to notifier me if the widget in row =n and col=2 changed or not.
    The QTableWidget::itemChanged() signal tells you when any item in the model changes. The item can tell you which row and column it represents. This is unaffected by changing the delegate.

  7. #7
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to cast QTableWidgetItem ti QLineEdit

    Why?
    If attached a QLineEdit in the cell, I lose cell/item signals, because QLineEdit in top of it.
    I think I achieved my goal:
    Qt Code:
    1. Store::Store(QWidget *parent) : QMainWindow(parent),
    2. ui(new Ui::Store)
    3. {
    4. ui->setupUi(this);
    5.  
    6. Store::setupBill();
    7. ....
    8. }
    9.  
    10.  
    11. void Store::setupBill()
    12. {
    13. .....
    14.  
    15. QLineEdit *item = new QLineEdit;
    16. ui->billTableWidget->setCellWidget(0, 1, item);
    17. Items items(ui);
    18. lst = new QStringList(items.getItems());
    19. cmpter = new QCompleter(*lst, item);
    20. cmpter->setCaseSensitivity(Qt::CaseInsensitive);
    21. cmpter->setCompletionMode(QCompleter::PopupCompletion);
    22. item->setCompleter(cmpter);
    23.  
    24. connect(cmpter, SIGNAL(activated(QString)), this, SLOT(suggestionItems(QString)));
    25. }
    To copy to clipboard, switch view to plain text mode 

    First I setup QTableWidget with an attached QLineEdit. Then I connected the activated signal of QCompleter with my SLOT void suggestionItems(QString):
    Qt Code:
    1. void Store::suggestionItems(QString itm)
    2. {
    3. PrintBill bill(ui);
    4. bill.getItem(itm);
    5. int row = ui->billTableWidget->rowCount();
    6.  
    7. ui->billTableWidget->insertRow(row);
    8. itemR->setText(QString::number(1));
    9. ui->billTableWidget->setItem(row - 1, 3, itemR);
    10. QLineEdit *item = new QLineEdit;
    11. ui->billTableWidget->setCellWidget(row, 1, item);
    12.  
    13. cmpter->setCaseSensitivity(Qt::CaseInsensitive);
    14. cmpter->setCompletionMode(QCompleter::PopupCompletion);
    15. item->setCompleter(cmpter);
    16. }
    To copy to clipboard, switch view to plain text mode 
    Witch my SLOT will fill the empty cells of current row, and then insert a new row attached with QLineEdit and QCompleter.

Similar Threads

  1. QMetaObject cast Error
    By nirab_25 in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2010, 08:58
  2. Why doesn't this cast?
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 19th November 2008, 19:28
  3. cast
    By mickey in forum General Programming
    Replies: 1
    Last Post: 12th July 2006, 11:10
  4. cast QString-GLenum
    By mickey in forum General Programming
    Replies: 1
    Last Post: 6th July 2006, 14:52
  5. Cast problem
    By yellowmat in forum Newbie
    Replies: 3
    Last Post: 7th February 2006, 15:57

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.