Results 1 to 6 of 6

Thread: qTableWidgetItem and qwtPlot subclassing

  1. #1
    Join Date
    Feb 2012
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default qTableWidgetItem and qwtPlot subclassing

    Hi to all,
    I have problem on qTableWidgetItem subclassing.
    I'm trying to display a qwtPlot (not a curve, only the axis) in the vertical header of a qTableWidget, using the tableWidget->setVerticalHeaderItem() function, because I need to have a lot of curves in the table widget, each one with his own plot, all of them in the same row, and a single axis scale in the vertical header of that row which is fixed when the scroll is used.

    It seems to me that everything is okay in my code, but the plot in the header does not appear in the table widget header.
    I tried to apply the qwtPlot-qTableWidgetItem in a normal cell in the table widget (not in the header), and the result is the same: nothing appears.

    This is my simple subclassing: nothing is added, only the qwtPlot became also a qTableWidgetItem.

    Qt Code:
    1. class MyPlot : public QTableWidgetItem, public QwtPlot
    2. {
    3. public:
    4. MyPlot();
    5. };
    6.  
    7. MyPlot::MyPlot() :
    8. {
    9. ;
    10. }
    To copy to clipboard, switch view to plain text mode 

    And this is the code I use:

    Qt Code:
    1. Viewgraph::Viewgraph(QWidget *parent)
    2. : QMainWindow(parent)
    3. {
    4. int samplesNumber = 200;
    5. QVector <double> yData;
    6. QVector <double> xData;
    7. QVector <double> zeroData;
    8.  
    9. MyPlot * axis = new MyPlot;
    10. QwtPlotCurve * zeroCurve = new QwtPlotCurve;
    11.  
    12. QTableWidget * tableWidget = new QTableWidget;
    13. tableWidget->setRowCount(1);
    14. tableWidget->setColumnCount(11);
    15. tableWidget->setRowHeight(0, 300);
    16. tableWidget->setColumnWidth(0, 30);
    17.  
    18. tableWidget->verticalHeader()->setResizeMode(QHeaderView::Fixed);
    19. tableWidget->verticalHeader()->resizeSection(0, 300);
    20.  
    21. yData.resize(samplesNumber);
    22. xData.resize(samplesNumber);
    23. zeroData.resize(samplesNumber);
    24.  
    25. for (int i = 0; i < samplesNumber; i++){
    26. yData[i] = sin((double) i/5);
    27. xData[i] = (double) i;
    28. zeroData[i] = (double) 0;
    29. }
    30.  
    31. MyPlot * plot = new MyPlot;
    32. QwtPlotCurve * plotCurve = new QwtPlotCurve;
    33. tableWidget->setColumnWidth(1, 60);
    34.  
    35. plot->enableAxis(QwtPlot::xBottom, false);
    36. plot->enableAxis(QwtPlot::yLeft, false);
    37. plot->setAxisScale(QwtPlot::yLeft, samplesNumber, 0, 0);
    38. plot->axisScaleDraw(QwtPlot::yLeft);
    39. plot->setAxisScale(QwtPlot::xBottom, -1, 1, 0);
    40. plotCurve->setOrientation(Qt::Horizontal);
    41. plotCurve->setSamples(yData, xData);
    42. plotCurve->setBaseline(0);
    43. plotCurve->attach(plot);
    44.  
    45. tableWidget->setCellWidget(0,1,plot);
    46.  
    47. axis->enableAxis(QwtPlot::xBottom, false);
    48. axis->setAxisScale(QwtPlot::xBottom, 0, 0, 0);
    49. axis->setAxisScale(QwtPlot::yLeft, samplesNumber, 0, 0);
    50. axis->axisScaleDraw(QwtPlot::yLeft);
    51. zeroCurve->setOrientation(Qt::Horizontal);
    52. zeroCurve->setPen(QColor(Qt::transparent));
    53. zeroCurve->setSamples(zeroData, xData);
    54. zeroCurve->attach(axis);
    55.  
    56. //tableWidget->setCellWidget(0,0,axis);
    57. tableWidget->setVerticalHeaderItem(0, axis);
    58.  
    59. tableWidget->show();
    60. }
    To copy to clipboard, switch view to plain text mode 

    If I was able to to put the "axis" plot in the vertical header my problem would be solved.
    Something is wrong with my code or is it impossible for some reason?

    And, if my way is not the right way, do you have some suggestion for a better mode?
    Thank you very much to all,
    Carlo

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qTableWidgetItem and qwtPlot subclassing

    Have you seen the plotmatrix example ?

    Uwe

  3. #3
    Join Date
    Feb 2012
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qTableWidgetItem and qwtPlot subclassing

    Hi Uwe,
    yes, I already knew the plotmatrix example, but it is not useful for my problem.
    Plot matrix is a nethod to sort in a matrix some plots (let me say: complete plots, with curve and axis), but it is not related to scroll function.
    What I need is:
    - to have a single fixed vertical axis with scale on the left side of the window (with no curve drawn),
    - more than one plot on the right side of the windows (each plot with no axis),
    - the horizontal scroll bar which work only on the plots of the right side of the window (treated as a whole, as in a normal table widget), so that the vertical axis on the left side remain fix in his position.

    Said in a simple way, I just need to put, in the vertical header, a qwtPlot.

  4. #4
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,325
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: qTableWidgetItem and qwtPlot subclassing

    Quote Originally Posted by chinalski View Post
    Said in a simple way, I just need to put, in the vertical header, a qwtPlot.
    The simple way is a QHBoxLayout with a QwtScaleWidget and a QScrollArea. The scrollarea contains the plot widgets organized in a grid layout.

    Using a QTableWidget introduces problems without any benefit.

    Uwe

  5. The following user says thank you to Uwe for this useful post:

    chinalski (4th May 2012)

  6. #5
    Join Date
    Feb 2012
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qTableWidgetItem and qwtPlot subclassing

    Okay, I'll try what you suggest.
    I'll let you know if I'm able to apply your solution.
    carlo

  7. #6
    Join Date
    Feb 2012
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: qTableWidgetItem and qwtPlot subclassing

    Thank you very much, Uwe,
    everything is ok now.
    I include my code, if this could be useful to someone.
    c

    Qt Code:
    1. #include <QTableWidget>
    2. #include <math.h>
    3. #include <QHeaderView>
    4. #include <QHBoxLayout>
    5. #include <QScrollArea>
    6. #include <QScrollBar>
    7. #include <QGridLayout>
    8.  
    9. #include "qwt_plot_curve.h"
    10. #include "qwt_plot.h"
    11. #include <qwt_scale_widget.h>
    12.  
    13. #include "viewgraph.h"
    14.  
    15. Viewgraph::Viewgraph(QWidget *parent)
    16. : QMainWindow(parent)
    17. {
    18. QWidget * window = new QWidget;
    19. window->resize(600,400);
    20.  
    21. QWidget * plotField = new QWidget(window);
    22. plotField->resize((50*20),600);
    23.  
    24. QScrollArea * scrollArea = new QScrollArea(window);
    25. QScrollArea * scaleArea = new QScrollArea(window);
    26. scrollArea->move(100, 0);
    27. scrollArea->resize(500,350);
    28. scrollArea->setWidget(plotField);
    29. scaleArea->move(0,0);
    30. scaleArea->resize(100,350);
    31. scaleArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    32.  
    33. int samplesNumber = 200;
    34. QVector <double> yData[20];
    35. QVector <double> xData[20];
    36.  
    37. QHBoxLayout *layout = new QHBoxLayout;
    38. layout->setSpacing(0);
    39.  
    40. QwtPlot * scale = new QwtPlot(window);
    41. scale->resize(40,600);
    42. scale->setAxisScale(QwtPlot::yLeft, samplesNumber, 0, 50);
    43. scale->enableAxis(QwtPlot::xBottom, false);
    44.  
    45. scaleArea->setWidget(scale);
    46.  
    47. QScrollBar * plotScrollBar = new QScrollBar;
    48. QScrollBar * scaleScrollBar = new QScrollBar;
    49. plotScrollBar = scrollArea->verticalScrollBar();
    50. scaleScrollBar = scaleArea->verticalScrollBar();
    51.  
    52. for (int j = 0; j < 20; j++) {
    53. yData[j].resize(samplesNumber);
    54. xData[j].resize(samplesNumber);
    55.  
    56. for (int i = 0; i < samplesNumber; i++){
    57. yData[j][i] = sin((double) i/5);
    58. xData[j][i] = (double) i;
    59. }
    60.  
    61. QwtPlot * plot = new QwtPlot;
    62. QwtPlotCurve * plotCurve = new QwtPlotCurve;
    63.  
    64. plot->enableAxis(QwtPlot::xBottom, false);
    65. plot->enableAxis(QwtPlot::yLeft, false);
    66.  
    67. plot->setAxisScale(QwtPlot::yLeft, samplesNumber, 0, 0);
    68. plot->axisScaleDraw(QwtPlot::yLeft);
    69.  
    70. plot->setAxisScale(QwtPlot::xBottom, -1, 1, 0);
    71. plot->setMaximumWidth(50);
    72.  
    73. plotCurve->setOrientation(Qt::Horizontal);
    74. plotCurve->setSamples(yData[j], xData[j]);
    75. plotCurve->setBaseline(0);
    76. plotCurve->attach(plot);
    77.  
    78. layout->addWidget(plot);
    79. }
    80.  
    81. QObject::connect(plotScrollBar, SIGNAL(valueChanged(int)),
    82. scaleScrollBar, SLOT(setValue(int)));
    83.  
    84. plotField->setLayout(layout);
    85. window->show();
    86. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 1
    Last Post: 11th August 2011, 06:35
  2. QTableWidgetItem
    By weixj2003ld in forum Qt Programming
    Replies: 3
    Last Post: 7th August 2011, 07:59
  3. QFrame error when subclassing QwtPlot
    By claudiacrb in forum Qwt
    Replies: 1
    Last Post: 30th April 2010, 08:04
  4. Replies: 6
    Last Post: 14th May 2009, 12:02
  5. Subclassing
    By joseph in forum Newbie
    Replies: 1
    Last Post: 25th February 2006, 14:06

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.