Results 1 to 8 of 8

Thread: How to combine two projects

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,321
    Thanks
    316
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to combine two projects

    A program cannot have two main() routines. The C runtime automatically looks for the method main() and uses it as the entry point for your program (that is, it calls that routine to start your program code's execution). If you renamed the "main()" from your Qt program to "main_g()" (as the name of your file implies), what did you do to cause it to execute? Nothing, apparently, since you don't see your GUI.

    You can't just naively dump all the source files for two programs into the same project, compile and link it (if it compiles and links at all, which it probably didn't, given that you renamed one of your main.cpp modules), and expect it to just run. The compiler, linker, and runtime can't read your mind. They do what you tell them to do. Looks like you told it to read serial data and nothing else.

    So, you need to take a step back and think about what you want your program to do, and how to get it to do that.

    0 - Start the program and get the serial port reader active and GUI visible
    1 - Read data from the serial port
    2 - Store that data somewhere so it can be plotted
    3 - Plot new data
    4 - Keep everything running in real time so both the serial port and plot stay "live"

    Start with the original main() for your custom plot program. Add the appropriate code from the other main() (serial reader) to the plot program's main(), then delete the other main.cpp from your project.

    In this main() you need to connect whatever signals your serial reader generates to a slot (which you will probably need to create and write code for) in your MainWindow class that will store the data somewhere (and you'll need to invent a data structure for that, too). You will also need to connect whatever the plot needs to update itself to a method in MainWindow. I don't know QCustomPlot in detail, but there is probably a slot that should be called to tell it that the data has changed and to replot itself.

    In order to keep the GUI and serial reader "live", you must do all of this within Qt's event loop. If you write something like a while() loop that repeatedly tries to read data from the serial port, this will lock up your GUI and nothing will be updated despite new data coming in. So use Qt's signal and slot mechanism and let the event loop tell your program (via a slot connected to a serial port signal) that new data is available. And let your program tell the plot (via a signal connected to a slot in the plot class) that the data has changed and it needs to update. And if neither of those things is happening for a while, then the event loop just runs and keeps the GUI active.
    <=== 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.

  2. #2
    Join Date
    Jun 2016
    Posts
    4
    Qt products
    Qt5

    Default Re: How to combine two projects

    Thank you! I have one last error, when I try to call the function that reads serial data, from mainwindow.cpp, and it says : C2664: ' QByteArray:: (QByteArrayDataPtr)': cannot convert argument 1 from 'QByteRef' to 'const char*'. No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called.
    The method from serialportreader.cpp is:
    Qt Code:
    1. QByteArray SerialPortReader::_read_callback()
    2. {
    3. QByteArray data = _serial->readAll();
    4. int sz = data.size();
    5.  
    6. for(int i=0;i<sz;i++)
    7. if((int)data[i] != 13 && (int)data[i] != 10)
    8. return (data[i]);
    9. }
    To copy to clipboard, switch view to plain text mode 
    And in mainwindow.cpp I have the following function that calls the above :
    Qt Code:
    1. void MainWindow::realtimeDataSlot()
    2. {
    3. double key = QDateTime::currentDateTime().toMSecsSinceEpoch()/1000.0;
    4. static double lastPointKey = 0;
    5. if (key-lastPointKey > 0.01)
    6. {
    7. SerialPortReader value;
    8. QByteArray value0=value._read_callback();
    9.  
    10. ui->customPlot->graph(0)->addData(key, value0);
    11. ui->customPlot->graph(1)->clearData();
    12. ui->customPlot->graph(1)->addData(key, value0);
    13.  
    14. ui->customPlot->graph(0)->rescaleValueAxis();
    15. lastPointKey = key;
    16. }
    17. ui->customPlot->xAxis->setRange(key+0.25, 8, Qt::AlignRight);
    18. ui->customPlot->replot();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 6th June 2016 at 18:47. Reason: missing [code] tags

  3. #3
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to combine two projects

    The return from QByteArray SerialPortReader::_read_callback() should look like :
    Qt Code:
    1. return data;
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Jun 2016
    Posts
    4
    Qt products
    Qt5

    Default Re: How to combine two projects

    I don't want to return all the data because on my Arduino data[1] is always 13 (carriage return) and data[2] is always 10 (new line), so I am only interested in data[0].

  5. #5
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to combine two projects

    The method SerialPortReader::_read_callback() is messy.
    1. Why it is defined as "return QByteArray" if it really returns only one byte ?
    2. If the data does not contain CRLF this method returns a random value if at all to compile.

Similar Threads

  1. Combine two QT projects
    By swamy in forum Qt Programming
    Replies: 4
    Last Post: 10th May 2016, 02:22
  2. Combine QGraphicsPathItem
    By mvbhavsar in forum Newbie
    Replies: 4
    Last Post: 11th February 2015, 12:08
  3. combine Symbian C++ and Qt
    By Manjula in forum Newbie
    Replies: 0
    Last Post: 9th March 2011, 14:53
  4. Replies: 2
    Last Post: 7th September 2010, 21:19
  5. Combine C++ Qt with PyQT
    By NoRulez in forum Qt Programming
    Replies: 1
    Last Post: 7th January 2009, 16:14

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.