Results 1 to 5 of 5

Thread: QTableWidget, QThread, signals and slots

  1. #1
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default QTableWidget, QThread, signals and slots

    Hello again.

    I working with QTableWidget. After setRowCount() and setColumnCount(), I have to do setItem(i, j, new QTableWidgetItem()). It's clear to me, obviously when I add some rows (or columns) I have to add more QTableWidgetItem. My question is do I have to remove QTableWidgetItem in this way
    Qt Code:
    1. tmp=m_ui->TableWidget->takeItem(i, j);
    2. delete tmp;
    To copy to clipboard, switch view to plain text mode 
    before I set row count to smaller number? Or its implemented inside setRowCount()?

    And second thing, I have to change GUI from QThread (display progress information in log window). For this I use signals and slots coonect looks like:
    Qt Code:
    1. connect(......., Qt::DirectConnection);
    To copy to clipboard, switch view to plain text mode 
    I use DirectConnection because it's very important to keep the same order in signal processing and emitting. It's works but sometime I get:
    QObject::connect: Cannot queue arguments of type 'QTextCursor'
    (Make sure 'QTextCursor' is registered using qRegisterMetaType().)
    in application output window, why?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget, QThread, signals and slots

    Do NOT touch GUI in a worker thread.
    Quote Originally Posted by docs
    Although QObject is reentrant, the GUI classes, notably QWidget and all its subclasses, are not reentrant. They can only be used from the main thread.
    I'm afraid you have to re-think the design.
    J-P Nurmi

  3. #3
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget, QThread, signals and slots

    I'm not touching GUI from thread.
    Qt Code:
    1. Answer=Interface->Read();
    2. if (Answer!=NULL)
    3. {
    4. char* tmp;
    5. tmp=new char[Answer->Len*2+1];
    6. convertByteTabToStr(Answer->Data, tmp, Answer->Len);
    7. emit setTextQLineEdit(m_ui->AnsLineEdit, tmp);
    8. emit appendQTextEdit(m_ui->LogTextEdit, QString("Ans --> ") +tmp);
    9. delete[] tmp;
    10. delete[] Answer->Data;
    11. delete Answer;
    12. }
    13. else
    14. {
    15. emit setTextQLineEdit(m_ui->AnsLineEdit, "");
    16. emit appendQTextEdit(m_ui->LogTextEdit, "Ans --> Can't read Ans.");
    17. char Text[]="Can't read Ans.";
    18. emit showMessageBox(Text, NULL, QMessageBox::Critical);
    19. }
    To copy to clipboard, switch view to plain text mode 
    I'm working with external device connected via RS232. I can't determine when the data will be came in, or how long I have to wait for answer (and answer length as well, simple I have to check when the data stop came in). From past I know that second thread is the best way (and the fasted too). Later I have to write another thread to listen on ethernet socket (it will be collect report from far away devices, and check the data). I can write data to file, and read the file in main thread then fill GUI widget, buy users don't like progress bars. They want to see how its going. Alternate method is function onIdle() (function that is calling when event queue is empty) and it's implemented in main thread (but it is not a really good idea). This is the reason why I chose the thread.
    What about the first question?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QTableWidget, QThread, signals and slots

    I'm confused. First of all, emitting a direct signal from a worker thread means that the GUI thread gets called in the worker thread context. Secondly, I presume QTextCursor is pretty much limited to the GUI thread since it deals with QFont, so it's not a registered meta type for a reason.

    As for the second question, yes QTableWidget::setRowCount() will cleanup items where appropriate.
    J-P Nurmi

  5. #5
    Join Date
    Dec 2008
    Posts
    13
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableWidget, QThread, signals and slots

    As for the second question, yes QTableWidget::setRowCount() will cleanup items where appropriate.
    Good, less work for me.

    Qt::BlockingQueuedConnection
    With this parameter program works fine, I think it's should be keep order.
    Last edited by kazek3018; 30th December 2008 at 21:29.

Similar Threads

  1. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  2. Problem with SpinBox signals and slots
    By ramstormrage in forum Newbie
    Replies: 4
    Last Post: 2nd May 2008, 01:45
  3. QThread - multi threaded signals and slots
    By rishid in forum Qt Programming
    Replies: 4
    Last Post: 30th March 2008, 01:47
  4. Signals and Slots Across Threads
    By themusicalguy in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 11:16
  5. signals and slots in plugins
    By anderl in forum Qt Programming
    Replies: 1
    Last Post: 10th October 2007, 13: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.