Results 1 to 8 of 8

Thread: Pass a parameter to new QWidget

  1. #1
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Pass a parameter to new QWidget

    Hello!

    I want to create a program, which plots graph. I am using QtCharts (thanks God its available in Qt 5.7). The idea is: there is a Widget (inher. from QWidget), I push a button and create new QWidget - window and there is plot, which is build already.
    But I want to re-plot it, so I put the int-number in QLineEdit on my new QWidget and I want the graph to re-plot, but the problem is that I already created a QWidget and graph is already there..
    How can I pass parameter to new QWidget in order to re-plot my graph there..?

    This is code:

    Qt Code:
    1. void Widget::plotAGraph()
    2. {
    3. QVBoxLayout *vertical = new QVBoxLayout();
    4. QHBoxLayout *horizontal = new QHBoxLayout();
    5. QPushButton *ok2 = new QPushButton("Close");
    6. QLineEdit *plotNumber = new QLineEdit("Step Number");
    7. QSlider *stepp=new QSlider(Qt::Horizontal);
    8. horizontal->addWidget(ok2);
    9. horizontal->addWidget(plotNumber);
    10. horizontal->addWidget(stepp);
    11.  
    12. QScatterSeries *series0 = new QScatterSeries();
    13. // series0->setName("Первое сечение");
    14. series0->setMarkerShape(QScatterSeries::MarkerShapeCircle);
    15. series0->setMarkerSize(15.0);
    16.  
    17. //here is parameter of my graph__and parameter step I want to change___
    18. int a=1;
    19. for(int i=step;i<step+50;i++){
    20. series0->append(a, obj.A[i]);
    21. a++;
    22.  
    23. }
    24. //_______________________________________________________________
    25.  
    26.  
    27. QChart *chart=new QChart;
    28.  
    29. chart->addSeries(series0);
    30. chart->setTitle("Сечение в первом разрезе");
    31. // chart->axisX()->setRange(0,90);
    32. // chart->axisY()->setRange(0,100);
    33. chart->createDefaultAxes();
    34. chart->axisX()->setRange(0,90);
    35. chart->axisY()->setRange(0,100);
    36.  
    37. QChartView *chartView = new QChartView(chart);
    38. chartView->setRenderHint(QPainter::Antialiasing);
    39.  
    40.  
    41. QWidget *newWindow=new QWidget();
    42. vertical-> addWidget(chartView);
    43. vertical-> addLayout(horizontal);
    44. newWindow-> setLayout(vertical);
    45.  
    46. newWindow->setGeometry(300,150,0,0);
    47. newWindow->resize(600,500);
    48.  
    49.  
    50. newWindow->show();
    51.  
    52.  
    53. connect(ok2,SIGNAL(clicked(bool)),newWindow,SLOT(replot()));
    54.  
    55. }
    56.  
    57. void Widget::replot()
    58. {
    59. //Doesnt work
    60. // int step = plotNumber.text().toInt();
    61. // how can I pass a parameter step in Loop in slot plotGraph and then to re-plot it all ?
    62. }
    To copy to clipboard, switch view to plain text mode 

    in file Widget.cpp I created object of class calculation and named it obj.
    In the class there is an Array A

    I create a plot:
    Qt Code:
    1. for(int i=step;i<step+50;i++){
    2. series0->append(a, obj.A[i]);
    3. a++;
    4. }
    To copy to clipboard, switch view to plain text mode 
    а - step along x
    А[i] - value accord. to the step

    Thanks in advance!
    Last edited by Blitzor DDD; 27th July 2016 at 20:39.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    I am afraid I didn't understand what you are actually asking for?

    You have an instance of "Widget" and you want to pass data to that object?

    Cheers,
    _

  3. #3
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    Ok, I will try to explain.
    I am using this: http://doc.qt.io/qt-5/qtcharts-scatt...t-example.html

    When I push the button on QWidget open new QWidget with plot like in the article.
    Then I would like to change my plot on new QWidget but the problem is when I create QWidget my plot is an argument of QWidget's object. and since then I am unable to change data of my plot.

    P.S.
    Or at least can I create a few graphs, independent from each other on QWidget from the start ?
    Last edited by Blitzor DDD; 28th July 2016 at 10:40.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    Well, can't you just keep a pointer to the object you want to change?

    Cheers,
    _

  5. #5
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    Well, can't you just keep a pointer to the object you want to change?
    I am sorry, definitely it is lack of knowledge from my side, but I see it this way: when I create new QWidget I create it with plot and in order to change it I must create new QWidget, right ?
    If there is likelihood to solve it using pointer (I didnt see it in the example of off. doc.) please write a code with a pointer and interaction on its base.
    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    Quote Originally Posted by Blitzor DDD View Post
    I am sorry, definitely it is lack of knowledge from my side, but I see it this way: when I create new QWidget I create it with plot and in order to change it I must create new QWidget, right ?
    
    No, the plot class has methods to change its data.

    Quote Originally Posted by Blitzor DDD View Post
    If there is likelihood to solve it using pointer (I didnt see it in the example of off. doc.) please write a code with a pointer and interaction on its base.
    Instead of using a local variable "chartView" you use a member variable in your "Widget" class.
    Then any method of "Widget" can access "chartView" and call its methods to change data.

    Cheers,
    _

  7. #7
    Join Date
    Jul 2016
    Posts
    41
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass a parameter to new QWidget

    Ok dann. Ich hab nicht alles verstanden, aber sowiese vielen Dank fur Ihre Hilfe

  8. #8
    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: Pass a parameter to new QWidget

    Ok dann. Ich hab nicht alles verstanden, aber sowiese vielen Dank fur Ihre Hilfe
    So fragen Sie mehr...
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 7th April 2014, 09:43
  2. How to pass events from QWidget covering QMainWindow?
    By SeeLook in forum Qt Programming
    Replies: 6
    Last Post: 4th February 2013, 18:10
  3. Replies: 3
    Last Post: 9th February 2011, 07:28
  4. Replies: 14
    Last Post: 1st December 2009, 20:45
  5. Replies: 6
    Last Post: 4th April 2006, 07:13

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.