Results 1 to 8 of 8

Thread: Qwt Problem

  1. #1
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question Qwt Problem

    I'm having a problem with my app crashing. I have a QTableWidget that has 2 columns that I am populating with a drag and drop event. If I plot everything in the table I have no problem. If I select a few rows and only try to plot them, I crash. Here is my slot that is connected to a "Plot" button. Can anyone tell what I am doing wrong?

    Qt Code:
    1. void cPlot2d::setup2dPlot()
    2. {
    3. QAbstractItemModel * model = this->mTableWidget->model();
    4. QItemSelectionModel * selectionModel = this->mTableWidget->selectionModel();
    5. QModelIndex index = model->index(0,0);
    6.  
    7. int totalRowCount = model->rowCount();
    8. bool select(false);
    9.  
    10. //If user selects any items this model contains it
    11. QItemSelection selection = selectionModel->selection();
    12. QModelIndexList list = selection.indexes();
    13.  
    14. //Number of items selected
    15. int selectCount = list.count();
    16. int numRowsSelected(0);
    17. for(int i=0;i<selectCount;++i)
    18. {
    19. if(list[i].column()==0)
    20. ++numRowsSelected;
    21. }
    22.  
    23. QwtArray<double> x;
    24. QwtArray<double> y;
    25. QwtArray<double> xSelect;
    26. QwtArray<double> ySelect;
    27.  
    28. if(totalRowCount > 0)
    29. {
    30. //Has any items been selected
    31. if(selectCount>0)
    32. {
    33. select = true;
    34. for(int i=0; i<selectCount; ++i)
    35. {
    36. //For MVC, get the data out of the model
    37. //Data is stored contiguously in list, so got to figure out what column data is in
    38. //x=0 y=1
    39. if(list.at(i).column() == 0)
    40. {
    41. xSelect.push_back(list.at(i).data().toDouble());
    42. }
    43. else
    44. {
    45. ySelect.push_back(list.at(i).data().toDouble());
    46. }
    47. }
    48. }
    49. //No items selected, load up entire table
    50. else
    51. {
    52.  
    53. for(int i=0; i<totalRowCount; ++i)
    54. {
    55. //For MVC, get the data out of the model
    56. x.push_back(model->data(index.sibling(i,0)).toDouble());
    57. y.push_back(model->data(index.sibling(i,1)).toDouble());
    58. }
    59. }
    60. // Set up Qwt Plot stuff
    61. QwtPlot * plot(new QwtPlot(this));
    62. plot->setTitle("2D Plot");
    63. // axis titles
    64. plot->setAxisTitle(QwtPlot::xBottom, "x");
    65. plot->setAxisTitle(QwtPlot::yLeft, "y");
    66.  
    67. // curve
    68. QwtPlotCurve * curve(new QwtPlotCurve("Curve 1"));
    69. curve->attach(plot);
    70. // data
    71. if(select)
    72. {
    73. //User selected data to plot
    74. curve->setData(xSelect, ySelect);
    75. }
    76. else
    77. {
    78. //Plot entire table
    79. curve->setData(x, y);
    80. }
    81. // pen
    82. curve->setPen(QPen(QColor(0, 0, 200)));
    83. // style
    84. curve->setStyle(QwtPlotCurve::Lines);
    85.  
    86. plot->replot();
    87.  
    88. cMainWindow::getInstance()->mWorkspace->addWindow(plot)->show();
    89.  
    90. }
    91. else
    92. {
    93. cout << " NO DATA TO PLOT!! " << endl;
    94. return;
    95. }
    96.  
    97. } // cPlot2d::setup2dPlot
    To copy to clipboard, switch view to plain text mode 
    Last edited by allensr; 21st November 2006 at 21:04.

  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: Qwt Problem

    Run the code through a debugger to see where exactly does it crash. QTableWidget has, by the way, convenience methods for manipulating selected items...
    J-P Nurmi

  3. #3
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qwt Problem

    JPN:

    I did a few things. One, I used your suggestion in using the convenience methods from TableWidget (changes in code below). When I run it from the command prompt I get:
    ASSERT failure in QReadWriteLock::unlock(): "Cannot unlock an unlocked lock", file c:\qt\4.1.4\src\corelib\thread\qreadwritelock.cpp, line 215
    QMutex::lock(): Deadlock detected in thread 2552

    When I run it through the debugger, it dies when it leaves this method. It looks like its having problem in a destructor -- deleting QModelIndex.

    Qt Code:
    1. void cPlot2d::setup2dPlot()
    2. {
    3. QAbstractItemModel * model = this->mTableWidget->model();
    4. QItemSelectionModel * selectionModel = this->mTableWidget->selectionModel();
    5. QModelIndex index = model->index(0,0);
    6.  
    7. int totalRowCount = model->rowCount();
    8. bool select(false);
    9.  
    10. //If user selects any items this model contains it
    11. QItemSelection selection = selectionModel->selection();
    12. QModelIndexList list = selection.indexes();
    13. QList<QTableWidgetItem *> selected = this->mTableWidget->selectedItems();
    14.  
    15. //Number of items selected
    16. int selectCount = list.count();
    17. int numRowsSelected(0);
    18. for(int i=0;i<selectCount;++i)
    19. {
    20. if(list[i].column()==0)
    21. ++numRowsSelected;
    22. }
    23.  
    24. QwtArray<double> x;
    25. QwtArray<double> y;
    26. QwtArray<double> xSelect;
    27. QwtArray<double> ySelect;
    28.  
    29. if(totalRowCount > 0)
    30. {
    31. //Have any items been selected
    32. if(selectCount>0)
    33. {
    34. select = true;
    35. int i(0);
    36. foreach(item, selected)
    37. {
    38. bool ok;
    39. double value = item->text().toDouble(&ok);
    40. //For MVC, get the data out of the model
    41. //Data is stored contiguously in list, so got to figure out what column data is in
    42. //x=0 y=1
    43. if(list.at(i).column() == 0)
    44. {
    45. if(ok && !item->text().isEmpty())
    46. xSelect.push_back(value);
    47. }
    48. else
    49. {
    50. if(ok && !item->text().isEmpty())
    51. ySelect.push_back(value);
    52. }
    53. ++i;
    54. }
    55. // }
    56. }
    57. //No items selected, load up entire table
    58. else
    59. {
    60.  
    61. for(int i=0; i<totalRowCount; ++i)
    62. {
    63. //For MVC, get the data out of the model
    64. x.push_back(model->data(index.sibling(i,0)).toDouble());
    65. y.push_back(model->data(index.sibling(i,1)).toDouble());
    66. }
    67. }
    68. // Set up Qwt Plot stuff
    69. QwtPlot * plot(new QwtPlot(this));
    70. plot->setTitle("2D Plot");
    71. // axis titles
    72. plot->setAxisTitle(QwtPlot::xBottom, "x");
    73. plot->setAxisTitle(QwtPlot::yLeft, "y");
    74.  
    75. // curve
    76. QwtPlotCurve * curve(new QwtPlotCurve("Curve 1"));
    77. curve->attach(plot);
    78. // data
    79. if(select)
    80. {
    81. //User selected data to plot
    82. curve->setData(xSelect, ySelect);
    83. }
    84. else
    85. {
    86. //Plot entire table
    87. curve->setData(x, y);
    88. }
    89. // pen
    90. curve->setPen(QPen(QColor(0, 0, 200)));
    91. // style
    92. curve->setStyle(QwtPlotCurve::Lines);
    93.  
    94. plot->replot();
    95.  
    96. cMainWindow::getInstance()->mWorkspace->addWindow(plot)->show();
    97.  
    98. }
    99. else
    100. {
    101. cout << " NO DATA TO PLOT!! " << endl;
    102. return;
    103. }
    104.  
    105. } // cPlot2d::setup2dPlot
    To copy to clipboard, switch view to plain text mode 

  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: Qwt Problem

    Sounds like you are manipulating the widget outside the main GUI thread, is this true?

    Thread Support in Qt:
    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.
    Last edited by jacek; 1st December 2006 at 20:27. Reason: changed [code] to [quote]
    J-P Nurmi

  5. #5
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qwt Problem

    Well, Im not sure. I am populating the TableWidget with data from a TreeWidget via a drag and drop. Is that manipulating the widget outside the main GUI thread? Again, I only have problems if I select items in the TableWidget.

  6. #6
    Join Date
    May 2006
    Posts
    33
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qwt Problem

    Not sure why, but removing any reference to QModelIndexList fixed my problems. Now works like a charm.

  7. #7
    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: Qwt Problem

    Ok good, then the problem is not with threads. I was suspecting you tried to do the plotting in a "worker" thread..

    To be honest, I can't find any pitfall in your code, but does it make any difference if you revise the function to look something like this:
    Qt Code:
    1. void cPlot2d::setup2dPlot()
    2. {
    3. int totalRowCount = this->mTableWidget->rowCount();
    4. QList<QTableWidgetItem *> selected = this->mTableWidget->selectedItems();
    5.  
    6. QwtArray<double> x;
    7. QwtArray<double> y;
    8.  
    9. if(totalRowCount > 0)
    10. {
    11. //Have any items been selected
    12. if(selected.count() > 0)
    13. {
    14. foreach(item, selected)
    15. {
    16. bool ok = false;
    17. double value = item->text().toDouble(&ok);
    18. //For MVC, get the data out of the model
    19. //Data is stored contiguously in list, so got to figure out what column data is in
    20. //x=0 y=1
    21. if(item->column() == 0)
    22. {
    23. if(ok && !item->text().isEmpty())
    24. x.push_back(value);
    25. }
    26. else
    27. {
    28. if(ok && !item->text().isEmpty())
    29. y.push_back(value);
    30. }
    31. }
    32. }
    33. //No items selected, load up entire table
    34. else
    35. {
    36. for(int i=0; i<totalRowCount; ++i)
    37. {
    38. //For MVC, get the data out of the model
    39. QTableWidgetItem* item0 = mTableWidget->item(i,0);
    40. QTableWidgetItem* item1 = mTableWidget->item(i,1);
    41. x.push_back(item0->text().toDouble());
    42. y.push_back(item1->text().toDouble());
    43. }
    44. }
    45. // Set up Qwt Plot stuff
    46. QwtPlot * plot(new QwtPlot(this));
    47. plot->setTitle("2D Plot");
    48. // axis titles
    49. plot->setAxisTitle(QwtPlot::xBottom, "x");
    50. plot->setAxisTitle(QwtPlot::yLeft, "y");
    51.  
    52. // curve
    53. QwtPlotCurve * curve(new QwtPlotCurve("Curve 1"));
    54. curve->attach(plot);
    55. // data
    56. curve->setData(x, y);
    57. // pen
    58. curve->setPen(QPen(QColor(0, 0, 200)));
    59. // style
    60. curve->setStyle(QwtPlotCurve::Lines);
    61.  
    62. plot->replot();
    63.  
    64. cMainWindow::getInstance()->mWorkspace->addWindow(plot)->show();
    65.  
    66. }
    67. else
    68. {
    69. cout << " NO DATA TO PLOT!! " << endl;
    70. return;
    71. }
    72. } // cPlot2d::setup2dPlot
    To copy to clipboard, switch view to plain text mode 

    All I did was cleaning up the unnecessary usage of model indexes which you actually shouldn't need while working with convenience views. If it still crashes, could you please post the backtrace from the debugger? And could you tell the exact version of Qt you are using as well?
    J-P Nurmi

  8. #8
    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: Qwt Problem

    Quote Originally Posted by allensr View Post
    Not sure why, but removing any reference to QModelIndexList fixed my problems. Now works like a charm.
    Ok, great that the problem was solved. Are you by the way using a recent version of Qt? It may even be a known/fixed bug. You can examine Qt related bugs in the Task tracker.
    J-P Nurmi

Similar Threads

  1. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  2. Problem with bitBlt
    By yellowmat in forum Newbie
    Replies: 1
    Last Post: 5th April 2006, 14:08
  3. fftw problem
    By lordy in forum General Programming
    Replies: 1
    Last Post: 16th March 2006, 21:36
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. use interesting QWT Library with QT3.X
    By raphaelf in forum Qwt
    Replies: 2
    Last Post: 23rd January 2006, 11:24

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.