Results 1 to 11 of 11

Thread: QModelIndex problem!

  1. #1
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QModelIndex problem!

    I incorporated the following example's methods into my code!
    Qt Code:
    1. #include <QtCore>
    2. #include <QtGui>
    3.  
    4. class Inserter : public QObject {
    5. public:
    6. Inserter(QStandardItemModel *m) : QObject() {
    7. model = m;
    8. }
    9. protected:
    10. void timerEvent(QTimerEvent *e){
    11. QString str = QDateTime::currentDateTime().toString();
    12. model->appendRow(new QStandardItem(str));
    13. }
    14. private:
    15. };
    16.  
    17. int main(int argc, char **argv){
    18. QApplication app(argc, argv);
    19. model.setColumnCount(1);
    20. lv.setModel(&model);
    21. lv.show();
    22. Inserter ins(&model);
    23. ins.startTimer(1000);
    24. return app.exec();
    25. }
    To copy to clipboard, switch view to plain text mode 

    The entries show up OK whilst in the vicinity of the starting code!

    However off in a thread I am doing the same code:

    Qt Code:
    1. QString qstr("landon 2nd");
    2.  
    3. model->appendRow(new QStandardItem(qstr));
    To copy to clipboard, switch view to plain text mode 

    However, I get the following errors at run time and no entry:

    Qt Code:
    1. QObject::connect: Cannot queue arguments of type 'QModelIndex'
    2. (Make sure 'QModelIndex' is registered using qRegisterMetaType().)
    3. QObject::connect: Cannot queue arguments of type 'QModelIndex'
    4. (Make sure 'QModelIndex' is registered using qRegisterMetaType().)
    To copy to clipboard, switch view to plain text mode 

    Somehow the thread is not connected to the QModelIndex used at the beginning

    I am feverishly researching the matter whilst reading about MVC.

    qRegisterMetaType<QString>(); --->>>didn't work!
    Last edited by landonmkelsey; 22nd August 2008 at 07:19. Reason: later thought

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex problem!

    You shouldn't touch the GUI in a non-GUI thread. You have to access your model through a queued connection, not directly.

  3. The following user says thank you to jacek for this useful post:

    landonmkelsey (22nd August 2008)

  4. #3
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default

    Thanks!

    Therefore, the following counts as a "gui thread" since it works!


    Qt Code:
    1. void timerEvent(QTimerEvent *e){
    2. QString str = QDateTime::currentDateTime().toString();
    3. model->appendRow(new QStandardItem(str));
    4. }
    To copy to clipboard, switch view to plain text mode 

    If the class inherits from QObject, maybe that "counts"

    Knowing full well, this probably wouldn't work, I programmed a facility for the thread to call a function back in the model's home turf.
    Qt Code:
    1. void tcpip::addQString(QString qstr)
    2. {
    3. model->appendRow(new QStandardItem(qstr));
    4. QApplication::processEvents();
    5. }
    To copy to clipboard, switch view to plain text mode 

    It didn't work! I'm going to have to thinks some more!

    I could put the strings in a list accessible to everybody and have still another thread update the model! But there it is again!

    This is like the concept of remote object invocation where no environment is involved!

    May have changed but .NET windows services cannot interact with windows-guis.

    A gui windows program CAN observe information produced by the Windows service!...HMMMM!
    Last edited by wysota; 3rd December 2009 at 11:47.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex problem!

    Quote Originally Posted by landonmkelsey View Post
    If the class inherits from QObject, maybe that "counts"
    No, it doesn't matter from what class it was derived. The important thing is where the object was created, as objects belong to the thread in which they were created (of course you can later move them between threads).

    Quote Originally Posted by landonmkelsey View Post
    Knowing full well, this probably wouldn't work, I programmed a facility for the thread to call a function back in the model's home turf.
    Let's start from the beginning. Why do you need a thread?

  6. The following user says thank you to jacek for this useful post:

    landonmkelsey (23rd August 2008)

  7. #5
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default

    I have 2 threads now

    (1) a thread that leavess the main mechanism of the program in its own thread so events can take place in the new QThread thread. We solved this problem in another thread last week. At that time I was doing non-model posts to a QListWidget. I am now getting "stuff" in the QListWidget(I've learned doesn't do MVC)

    (2) one model of tcpip/tcp requires (as a concurrent server) a thread to handle each client that "calls" in!

    Each thread handles each client. Prior to Qt 4 I was using a Posix thread that has worked flawlessly for years! I scrapped the Posix thread and am now using QThread everywhere!

    In any case, I am learning a lot. I like the MVC and am also reading along with practice!

    Thanks for your patience!

    Remember the MVC example you sent??? Well I incorporated it into my tcpip startup to see if at least it would generate some display! It hasn't yet!

    Qt Code:
    1. class Inserter : public QObject {
    2. public:
    3. Inserter(QStandardItemModel *m) : QObject() {
    4. model = m;
    5. }
    6. protected:
    7. void timerEvent(QTimerEvent *e){
    8. QString str = QDateTime::currentDateTime().toString();
    9. model->appendRow(new QStandardItem(str));
    10. }
    11. private:
    12. };
    To copy to clipboard, switch view to plain text mode 

    Timer thing didn't work either:
    Qt Code:
    1. void tcpip::StartTCPServer()
    2. {
    3. ui.listWidgetServer->addItem(QString("server1 StartTCPServer"));
    4.  
    5. ui.listViewServer->setModel(model);
    6. model->setColumnCount(1);
    7. QString str("landon is first");
    8. model->appendRow(new QStandardItem(str));
    9.  
    10. QString str2("landon is second");
    11. model->appendRow(new QStandardItem(str2));
    12. QApplication::processEvents();
    13. bool ok = true;
    14. QString qstr = ui.lineEditTCPIPServerPortServer->text();
    15. port = qstr.toInt(&ok);
    16. //qDebug()<<" server port "<<port;
    17. ui.lineEditServerStatus->setText("server started");
    18.  
    19. QApplication::processEvents();
    20.  
    21. tcpipThread* ptcpipThreadStart = new tcpipThread(ui, port, this);
    22. ptcpipThreadStart->start();
    23.  
    24. Inserter ins(model);
    25. ins.startTimer(1000);
    26.  
    27.  
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class Inserter : public QObject {
    2. public:
    3. Inserter(QStandardItemModel *m) : QObject() {
    4. model = m;
    5. }
    6. protected:
    7. void timerEvent(QTimerEvent *e){
    8. QString str("hello");
    9. model->appendRow(new QStandardItem(str));
    10. qDebug()<<"hello";
    11. }
    12. private:
    13. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 3rd December 2009 at 11:47.

  8. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex problem!

    I suggest redesigning the original code. Starting one thread per client is a doubtful solution. For 100 clients you'll have 101 threads... That's quite a stress on the system - context switching will kill you when the number of clients increases.

  9. #7
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QModelIndex problem!

    The point here is to learn something!

    If I had surrendered/redesigned every time I ran into a problem, I'd never have learned anything!

    The code is unimportant here since my ultimate aim is to incorporate the bridge pattern. Ultimately the user will be able to choose the means of server concurrency
    via the bridge pattern. In C# this is straightforward!

    My code in C# is a great testament to the value of patterns!

    Redesign? Why? The user chooses ultimately!

    I started out trying to incorporate Qt4 for gui into my already running program!

    The means of concurrency in TCP/IP is controversial anyway!

    See great work of the deceased W Richard Stevens! www.kohala.com
    Last edited by landonmkelsey; 25th August 2008 at 19:52. Reason: add on

  10. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex problem!

    Quote Originally Posted by landonmkelsey View Post
    The point here is to learn something!

    If I had surrendered/redesigned every time I ran into a problem, I'd never have learned anything!
    You would learn to avoid bad designs and create good ones.

    Redesign? Why? The user chooses ultimately!
    Because the solution is neither scalable nor modular nor straightforward.

    I started out trying to incorporate Qt4 for gui into my already running program!
    So what's keeping you from rewriting some parts of the application to simplify the overall solution instead of wrestling against problems related to mixing two different technologies that only need a minor change to interoperate perfectly?

    The means of concurrency in TCP/IP is controversial anyway!
    I'm not sure what you mean here, but there are numerous designs related to obtaining concurrency in TCP connections - just look at available operation modes of the Apache webserver. If you want multithreading, you are welcome to use it but do it properly, for example by using a thread pool to keep resource usage at a reasonable level while keeping your system responsive.

  11. #9
    Join Date
    Aug 2008
    Posts
    38
    Thanks
    19
    Qt products
    Qt4
    Platforms
    Unix/X11

    Talking Re: QModelIndex problem!

    the main point is !

    My C# code uses a bridge pattern to allow the user programmer to select which "client handler" he/she wants/supplies! One can choose the thread pool or 1 client one thread generated on "call-in"

    One may even choose an iterative server!

    I wanted to generate C++ code to do the same thing! Since C++ does not have an "interface", I would have to use an ABC!

    I'll solve the problem myself! Thanks for your help!

    I quess C++ will wither and die! : ( as Java and VB)

    done!
    Last edited by landonmkelsey; 28th August 2008 at 19:50. Reason: missed a word

  12. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QModelIndex problem!

    Quote Originally Posted by landonmkelsey View Post
    Since C++ does not have an "interface", I would have to use an ABC!
    You don't need a separate keyword for everything. An interface is a special kind of ABC.

  13. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QModelIndex problem!

    Quote Originally Posted by landonmkelsey View Post
    My C# code uses a bridge pattern to allow the user programmer to select which "client handler" he/she wants/supplies! One can choose the thread pool or 1 client one thread generated on "call-in"
    I quess C++ will wither and die! : ( as Java and VB)
    The language used is independent of the design. If you have some design in C#, you can implement the same design in other languages.

    On the other hand a bad design is bad independent of the language used to implement it.

    You are trying to bridge two technologies that use different concepts. This is possible but prone to errors and that's exactly what you are experiencing. So you can either struggle with that or change one of the concepts.

    As for interfaces - interface is an abstract term. Each class is an interface, a class containing virtual methods is an interface that can be realized in different ways by more than one class, a class containing pure abstract methods is an abstract interface that has to be implemented by other class(es) (an ABC).

Similar Threads

  1. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 19:14
  2. Problem with TreeView
    By init2null in forum Qt Programming
    Replies: 8
    Last Post: 25th May 2008, 10:56
  3. QAbstractItemModel newbie question
    By okellogg in forum Qt Programming
    Replies: 14
    Last Post: 18th February 2008, 13:30
  4. Problem: QThead and QTableview
    By ederbs in forum Qt Programming
    Replies: 5
    Last Post: 9th November 2007, 11:17
  5. Replies: 6
    Last Post: 21st September 2007, 14:51

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.