Results 1 to 5 of 5

Thread: Non-comprehensive problem about Qwt in OO language

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Non-comprehensive problem about Qwt in OO language

    Hello!

    I'm having a problem about passing Qwt to a Object-oriented language style in my software. Let me explain.

    My goal, now, is to create a software which receives data from a socket client, "translate it" and pass the y coordinates to three graphs simultaneously. in fact, I already have this software and it works fine.

    The problem is that it is not written in traditional c++ object oriented style when it comes about using Qwt to create the 3 graphs, i.e. the instantiation of each graph is done "manually", not by simply creating the object from a class that create graphs.

    So what I have today in my MainWindow, where the creation of the graphs is called, is this:
    Qt Code:
    1. void MainWindow::on_Start_released()
    2. {
    3. ui->cmd->append("Start");
    4.  
    5. plot = new QwtPlot(QwtText("Graph 1"));
    6. plot->setParent(ui->grafico); //Liga o grafico à widget
    7.  
    8. //Dimensões do gráfico dentro do widget
    9. plot->setGeometry(0,0,1020,200);
    10. plot->setAxisScale(QwtPlot::yLeft,0,300);
    11. plot->setAxisScale(QwtPlot::xBottom,0.0,150);
    12. //plot->setAxisTitle(QwtPlot::xBottom, "Tempo [s]"); //Falta espaço. Tentar colocar na borda do eixo, não embaixo
    13. //plot->setAxisScale(QwtPlot::xBottom, d_interval.minValue(), d_interval.maxValue()); //faz a mudança dos números no eixo X
    14.  
    15. //Coloca grid
    16. QwtPlotGrid *grid = new QwtPlotGrid();
    17. grid->setPen(QPen(Qt::gray, 0.0, Qt::DotLine));
    18. grid->enableX(true);
    19. grid->enableXMin(true);
    20. grid->enableY(true);
    21. grid->enableYMin(false);
    22. grid->attach(plot);
    23.  
    24. //plot->axisAutoScale(1);
    25. //plot->axisAutoScale(1);
    26. plot->show();
    27.  
    28. curva = new QwtPlotCurve("ECG"); //Antes: "Sine"
    29. curva->setStyle(QwtPlotCurve::Lines);
    30. curva->setPen(QPen(Qt::green)); //Cor da linha do gráfico (Cada um com sua cor?)
    31. curva->attach(plot);
    32. /*******************************************************/
    33. plot2 = new QwtPlot(QwtText("Graph 2"));
    34. plot2->setParent(ui->grafico2); //Liga o grafico à widget
    35.  
    36. //Dimensões do gráfico dentro do widget
    37. plot2->setGeometry(0,0,1020,200);
    38. plot2->setAxisScale(QwtPlot::yLeft,0,300);
    39. plot2->setAxisScale(QwtPlot::xBottom,0.0,150);
    40. //plot->setAxisTitle(QwtPlot::xBottom, "Tempo [s]"); //Falta espaço. Tentar colocar na borda do eixo, não embaixo
    41. //plot->setAxisScale(QwtPlot::xBottom, d_interval.minValue(), d_interval.maxValue()); //faz a mudança dos números no eixo X
    42.  
    43. //Coloca grid
    44. QwtPlotGrid *grid2 = new QwtPlotGrid();
    45. grid2->setPen(QPen(Qt::gray, 0.0, Qt::DotLine));
    46. grid2->enableX(true);
    47. grid2->enableXMin(true);
    48. grid2->enableY(true);
    49. grid2->enableYMin(false);
    50. grid2->attach(plot2);
    51.  
    52. //plot->axisAutoScale(1);
    53. //plot->axisAutoScale(1);
    54. plot2->show();
    55.  
    56. curva2 = new QwtPlotCurve("ECG"); //Antes: "Sine"
    57. curva2->setStyle(QwtPlotCurve::Lines);
    58. curva2->setPen(QPen(Qt::green)); //Cor da linha do gráfico (Cada um com sua cor?)
    59. curva2->attach(plot2);
    60. /*******************************************************/
    61. plot3 = new QwtPlot(QwtText("Graph 3"));
    62. plot3->setParent(ui->grafico3); //Liga o grafico à widget
    63.  
    64. //Dimensões do gráfico dentro do widget
    65. plot3->setGeometry(0,0,1020,200);
    66. plot3->setAxisScale(QwtPlot::yLeft,0,300);
    67. plot3->setAxisScale(QwtPlot::xBottom,0.0,150);
    68. //plot->setAxisTitle(QwtPlot::xBottom, "Tempo [s]"); //Falta espaço. Tentar colocar na borda do eixo, não embaixo
    69. //plot->setAxisScale(QwtPlot::xBottom, d_interval.minValue(), d_interval.maxValue()); //faz a mudança dos números no eixo X
    70.  
    71. //Coloca grid
    72. QwtPlotGrid *grid3 = new QwtPlotGrid();
    73. grid3->setPen(QPen(Qt::gray, 0.0, Qt::DotLine));
    74. grid3->enableX(true);
    75. grid3->enableXMin(true);
    76. grid3->enableY(true);
    77. grid3->enableYMin(false);
    78. grid3->attach(plot3);
    79.  
    80. //plot->axisAutoScale(1);
    81. //plot->axisAutoScale(1);
    82. plot3->show();
    83.  
    84. curva3 = new QwtPlotCurve("ECG"); //Antes: "Sine"
    85. curva3->setStyle(QwtPlotCurve::Lines);
    86. curva3->setPen(QPen(Qt::green)); //Cor da linha do gráfico (Cada um com sua cor?)
    87. curva3->attach(plot3);
    88.  
    89. Server.StartServer();
    90. }
    To copy to clipboard, switch view to plain text mode 

    (Sorry for the Portuguese comments, by I imagine that you will understand the code anyway ;] )

    So as you can see, I have to create each of the 3 graphs manually, which is not the OO style. And I want, therefore, to transform that code in this:

    Qt Code:
    1. plot->createSimple(ui->grafico,"Graph 1","green");
    2. plot2->createSimple(ui->grafico2,"Graph 2","blue");
    3. plot3->createSimple(ui->grafico3,"Graph 3","red");
    To copy to clipboard, switch view to plain text mode 

    In this situation, I create the graphs from the "graf" class usign the function "createSimple", pointing each of the graphs to a specific QWidget, giving it a name and setting the color of line. Just to make things even more simple, here is the function createSimple as it is today:

    Qt Code:
    1. void graf::createSimple(QWidget *a, QString b, QString c)
    2. {
    3.  
    4. gra = new QwtPlot(QwtText(b));
    5. gra->setParent(a); //Liga o grafico à widget
    6.  
    7. //Dimensões do gráfico dentro do widget
    8. gra->setGeometry(0,0,1020,200);
    9. gra->setAxisScale(QwtPlot::yLeft,0,300);
    10. gra->setAxisScale(QwtPlot::xBottom,0.0,150);
    11. //plot->setAxisTitle(QwtPlot::xBottom, "Tempo [s]"); //Falta espaço. Tentar colocar na borda do eixo, não embaixo
    12. //plot->setAxisScale(QwtPlot::xBottom, d_interval.minValue(), d_interval.maxValue()); //faz a mudança dos números no eixo X
    13. //plot->axisAutoScale(1);
    14. //plot->axisAutoScale(1);
    15.  
    16. //Coloca grid
    17. grid = new QwtPlotGrid();
    18. grid->setPen(QPen(Qt::gray, 0.0, Qt::DotLine));
    19. grid->enableX(true);
    20. grid->enableXMin(true);
    21. grid->enableY(true);
    22. grid->enableYMin(false);
    23. grid->attach(gra);
    24.  
    25. gra->show();
    26.  
    27. linha = new QwtPlotCurve("ECG");
    28. linha->setStyle(QwtPlotCurve::Lines);
    29. linha->setPen(QPen(c));
    30. linha->attach(gra);
    31. }
    To copy to clipboard, switch view to plain text mode 

    But here is where the problem begins, and they are two problems.

    First, I simply can't understand why I can't instantiate (I hope this is the technical word for that) the variables "gra", "linha" [the line] and "grid"

    Qt Code:
    1. QwtPlot *gra;
    2. QwtPlotCurve *linha;
    To copy to clipboard, switch view to plain text mode 

    in the "graf.h" file; I have to instantiate them always in the "graph.cpp" file, which is not the correct way of doing it and, btw, its probably the source of all of my problems. If I try to instantiate the variables in the "graf.h" file, the compilation runs O.K., but when I start the program as soon as I click in the Start button to make the graphs appear, the software crash and it don't say what happened. So in order to make my software works to this point, I have always to instantiate in the .cpp file, what is wrong.

    So, my first question: is there a official problem about instantiating Qwt variables in the .h file? Why can't and do that in my software?

    ---
    Second and most annoying problem: once instantiating the variables in the .cpp file, I simply can't make the lines fit in the correct graph. Rather, they insist always to appear in the last graph in the window. Let me explain.

    Here is the code I use to "translate" the string of data from the socket client to Y variables to each of the Qwt graph in the version that works fine:

    .h file:
    Qt Code:
    1. public:
    2. std::vector<double> xs1;
    3. std::vector<double> ys1;
    4. std::vector<double> xs2;
    5. std::vector<double> ys2;
    6. std::vector<double> xs3;
    7. std::vector<double> ys3;
    8. double cur_x1, cur_x2, cur_x3;
    9.  
    10. QString str1, str2, str3;
    11. int str1X, str2X, str3X;
    To copy to clipboard, switch view to plain text mode 

    .cpp file:

    Qt Code:
    1. leitura = QString(socket->readLine());
    2. str1 = leitura.section(' ',0,0);
    3. str1X = str1.toInt(NULL);
    4. qDebug() << "1 col:" << str1X << endl;
    5. str2 = leitura.section(' ',1,1);
    6. str2X = str2.toInt(NULL);
    7. qDebug() << "2 col:" << str2X << endl;
    8. str3 = leitura.section(' ',2,2);
    9. str3X = str3.toInt(NULL);
    10. qDebug() << "3 col:" << str3X << endl;
    11.  
    12. ys1.push_back(str1.toDouble(NULL));
    13. xs1.push_back(cur_x1++);
    14.  
    15. pmw->curva->setData(&xs1[0],&ys1[0],xs1.size());
    16. pmw->plot->replot();
    17.  
    18. ys2.push_back(str2.toDouble(NULL));
    19. xs2.push_back(cur_x2++);
    20.  
    21. pmw->curva2->setData(&xs2[0],&ys2[0],xs2.size());
    22. pmw->plot2->replot();
    23.  
    24. ys3.push_back(str3.toDouble(NULL));
    25. xs3.push_back(cur_x3++);
    26.  
    27. pmw->curva3->setData(&xs3[0],&ys3[0],xs3.size());
    28. pmw->plot3->replot();
    To copy to clipboard, switch view to plain text mode 

    So let me explain: "leitura" reads the entire line that the socket client receives, and str1, str2 and str3 receives the three columns of data from "leitura", each of them with the Y coordinates for each of the graphs. Later each of the "ys" receives the data from "str1, str2 and str3" converted to double. Finally, a MainWindow vector called "*pmw" acess the varibles "curva" and "plot" from MainWindow (see first code) and ask them to execute the functions "setData()" and "replot()".


    As I sad earlier, this code is fine. The problem is when I try to execute a similar version using OO for the Qwt plot.

    Theorically, everything that I should do of different, if the variables "gra" and "linha" were instantiated in the "graf.h", would be to change the pmw path to something like this:

    Qt Code:
    1. pmw->plot->linha->setData(&xs1[0],&ys1[0],xs1.size());
    2. pmw->plot->replot();
    3.  
    4. pmw->plot2->linha->setData(&xs2[0],&ys2[0],xs2.size());
    5. pmw->plot2->replot();
    6.  
    7. pmw->plot3->linha->setData(&xs3[0],&ys3[0],xs3.size());
    8. pmw->plot3->replot();
    To copy to clipboard, switch view to plain text mode 

    [continue in the next post]

    [continuing]

    But if the variables are not instantiated in the "graf.h", they are not found by the "pmw" and this code is found useless. What I tried to do, than, was to create functions in "graph.cpp" that were capable of changing the value of this variables despite they were instantiated in the .cpp file (see first problem). But always when I try to do so, the software confuses itself and begin to plot only the last of the graphs that was instantiated.


    So for example, if the code is put entirely as it should, instead each of the graphs receives each of the data and show the curve, only the third graph plots the curve receiving its values. The other two remain "silent". Now if I create only two graphs in the MainWindow.cpp:

    Qt Code:
    1. plot->createSimple(ui->grafico,"Graph 1","green");
    2. plot2->createSimple(ui->grafico2,"Graph 2","blue");
    3. //plot3->createSimple(ui->grafico3,"Graph 3","red");
    To copy to clipboard, switch view to plain text mode 

    but I lat the code for reading the column the same (including the changes to plot3, that was not instantiaded), than the graph3 do'nt appear in the software, the graph1 doesn't show anything and the graph2 plots the data from graph3!!

    And so on, doesn't matter how many different functions I use to call setData(), it will always plot only the last graph while the others will remain clear.



    Do anybody know how to solve my 2 problems? Probably by solving the first one the second goes away too.

    Sorry for the immense post, but I think it was necessary to explain my problem exactly. I spend more than 6 hours by now trying to solve it and I'm beginning to become borrowed :P


    Thanks for your time! =]

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

    Default Re: Non-comprehensive problem about Qwt in OO language

    Quote Originally Posted by Momergil View Post
    So as you can see, I have to create each of the 3 graphs manually, which is not the OO style.
    There is nothing wrong with the original code and it absolutely not against OO. But even if - OO is not a synonym for good.

    Quote Originally Posted by Momergil View Post
    If I try to instantiate the variables in the "graf.h" file, the compilation runs O.K., but when I start the program as soon as I click in the Start button to make the graphs appear, the software crash and it don't say what happened.
    Implementing methods in a .h file doesn't make any sense beside if you want to inline a method. So I guess you want to do some sort of instantiation together with the declaration of a class members - but this is not C++ ( constructors are intended for this ). But this is a detail of the language and also has nothing to do with the OO paradigm.

    Sorry for not giving you a more detailed answer for your second question, but you didn't post the deciding parts of your code - general thoughts about OO are not important here.

    Uwe

  3. #3
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Non-comprehensive problem about Qwt in OO language

    Hello, Uwe!

    First, thanks for the reply. Let me see what I can do for you:

    Quote Originally Posted by Uwe View Post
    There is nothing wrong with the original code and it absolutely not against OO. But even if - OO is not a synonym for good.
    Well, I agree that the code itself is fine and that it is not necessary for me to create it using only OO. In fact, theorically the unique thing that really matters in a software is that it works fine, independent of the code that it uses. But, you know, when you are doing a software not for free, but being payed by a company that gives you money to do a good work, it is at least expected that I'm gonna do something correct. And, by the way that I learned C++ some time ago, the correct way of doing this would be by using OO. Imagine if my boss (who is also an engineering and therefore has an idea of what is a good, organized code programming) decides to see my software and he sees a mess. That certainly wouldn't be good for me! More than that, it's still a way of learning something more, makes to code become more organized (what in programming is always good) and so on. So I really thing it would be a good think if I change the code to a OO standart.


    Quote Originally Posted by Uwe View Post
    Implementing methods in a .h file doesn't make any sense beside if you want to inline a method. So I guess you want to do some sort of instantiation together with the declaration of a class members - but this is not C++ ( constructors are intended for this ). But this is a detail of the language and also has nothing to do with the OO paradigm.
    I'm not sure if I understood what you wanted to say with this comment, so I'm not able to reply it appropriately. But giving the few that I think I understood, I may say that I have the impression that you didn't understand my point. So let me revise what I want:

    In OO languages we have a .h and a .cpp (in case of C++). In the header, we instantiate the methods and variables that we specify and with which we are gonna work with in the .cpp. So for example, here comes a .h of my software:

    Qt Code:
    1. #ifndef MYSERVER_H
    2. #define MYSERVER_H
    3. #include "myclient.h"
    4. #include <iostream>
    5. #include <QTcpServer>
    6. #include <QTcpSocket>
    7. #include <QAbstractSocket>
    8. #include <QtCore>
    9. #include <QtGui>
    10.  
    11. class myserver : public QTcpServer
    12. {
    13.  
    14. Q_OBJECT
    15. public:
    16. explicit myserver(QObject *parent = 0);
    17. void StartServer();
    18. void CloseServer();
    19.  
    20. protected:
    21. void incomingConnection(int handle);
    22. signals:
    23. };
    24.  
    25. #endif // MYSERVER_H
    To copy to clipboard, switch view to plain text mode 

    and part of its .cpp:

    Qt Code:
    1. #include "myserver.h"
    2. #include "mainwindow.h"
    3. #include "ui_mainwindow.h"
    4. #include <QTextEdit>
    5. extern MainWindow *pmw;
    6.  
    7. myserver::myserver(QObject *parent) :
    8. QTcpServer(parent)
    9. {
    10. }
    11.  
    12. void myserver::StartServer()
    13. {
    14. //Aguarda conexão de qualquer IP na porta 1234
    15. if(listen(QHostAddress::Any,1234))
    16. {
    17. qDebug() << "Server Started";
    18. }
    19. else
    20. {
    21. qDebug() << "Server Not Started";
    22. }
    23. }
    24.  
    25. void myserver::CloseServer()
    26. {
    27. pmw->ui->cmd->append("Server Stoped");
    28. }
    29.  
    30. void myserver::incomingConnection(int handle)
    31. {
    32. myclient *client = new myclient(this);
    33. client->SetSocket(handle);
    34. }
    To copy to clipboard, switch view to plain text mode 

    Now its true that I can instantiate the variables directly in the .cpp and work with them from there. But in this case, the Qt is incapable of recognizing those variables as part of the class, and its not any miracle: that is not how standard C++ OO language is supposed to be.

    When it comes about my Qwt "graf" class, I only can instantiate my variables QwtPlot, QwtGrid and so forth in the .cpp, because when I do it as it should be, in the header file, it compiles O.K. but crashes when the graphs are created (when I click a "Start" button).

    Quote Originally Posted by Uwe View Post
    Sorry for not giving you a more detailed answer for your second question, but you didn't post the deciding parts of your code - general thoughts about OO are not important here.

    Uwe
    About not putting the "deciding parts of your code" regarding the second question, well, I must confess that I'm lost. Giving what I know about my problem, the parts that I put are the ones that really mater; everything else is in the description. So which exactly are this parts that you are talking about? If you thing it will be fine, I may preset the entire "graf" class, but giving the hundreds of changes that I did trying to make it work, it would probably give more problems than helps.

    I must go now so I can't give more details.

    Thank you anyway.

    Martin/Momergil

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

    Default Re: Non-comprehensive problem about Qwt in OO language

    Quote Originally Posted by Momergil View Post
    Well, I agree that the code itself is fine and that it is not necessary for me to create it using only OO. In fact, theorically the unique thing that really matters in a software is that it works fine, independent of the code that it uses. But, you know, when you are doing a software not for free, but being payed by a company that gives you money to do a good work, it is at least expected that I'm gonna do something correct.
    You also have to do things "right", when you are doing free software. In case of a long term project like Qwt with many thousands of unknown users and applications a good class design is even much more important as in any application code, that is running in a limited and well known environment.

    But this has mot much to do with your problem.

    Quote Originally Posted by Momergil View Post
    In OO languages we have a .h and a .cpp (in case of C++). In the header, we instantiate the methods and variables that we specify and with which we are gonna work with in the .cpp.
    No this is declaration - instantiation is something completely different. Maybe this is the reason for your confused posting. Is it possible, that your question is simply about class members vs. global variables ?

    The original code ( the one you don't like ) instantiates the plot widgets and its items in MainWindow::on_Start_released - in your code they are instantiated in graf::createSimple. The difference is, that MainWindow::on_Start_released instantiates 3 plots in advance and your code instantiates the plots on demand one by one.

    The first solution definitely makes sense when you want to have a fixed layout - yours will be better if you want to have a user interface with an unknown number of plots, that dynamically appear ( and maybe disappear ).

    Quote Originally Posted by Momergil View Post
    Now its true that I can instantiate the variables directly in the .cpp and work with them from there. But in this case, the Qt is incapable of recognizing those variables as part of the class, and its not any miracle: that is not how standard C++ OO language is supposed to be.
    Before you get lost in whatever terminology - what about posting a small piece of code demonstrating what you mean by "instantiation" and what doesn't work for you ?

    Quote Originally Posted by Momergil View Post
    So which exactly are this parts that you are talking about?
    Your application crashes not because of OO, but because of very basic things like your are running on uninitialized pointers. So, when you start your debugger you will see what pointer it is and then you can show your code dealing with this pointer. From the discussion above I guess it's enough to show the declaration and the instantiation of this pointer in your code.

    Uwe

  5. #5
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Non-comprehensive problem about Qwt in OO language

    Hello, Uwe!

    First, thanks for your time in trying to help me once again.

    But second, I would like to happily say that, despite I don't know how, now my code is working - in exactly the way that it wasn't working before! For a unknown reason now I'm able to do declaration in the header file and the graph is running O.K. in the way that I wanted and, as I predicted, it was just a matter of declarating the variables in the header and than everything else would run O.K.. About how I did it, it was just a matter of knowing what could be done with a simpler software, than change the older, correct version in accord to it.

    About your comments, I thank you once again for them. Sorry if my confusion between "declaration" and "instantiation" made my comments confusing, and I agree with you about the issue on having organized programs.

    Once again thank you for everything.

    Martin/Momergil

Similar Threads

  1. Arabic language support problem (right to left)
    By sam_er83 in forum Qt Tools
    Replies: 1
    Last Post: 14th May 2012, 09:48
  2. Arabic language support problem (right to left)
    By sam_er83 in forum Qt Programming
    Replies: 0
    Last Post: 9th September 2009, 07:00
  3. Replies: 0
    Last Post: 19th August 2007, 05:47
  4. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 14:18

Tags for this Thread

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.