Results 1 to 3 of 3

Thread: Instantiate the QApplication object first

  1. #1
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Question Instantiate the QApplication object first

    Hello,

    I have a problem using QSerialPort.
    If I try to set the serial connection parameters directly in the main I get the message:
    QApplication::qAppName: Please instantiate the QApplication object first
    If I try to solve this with adding line 3 in the code I get another error:
    invalid conversion from 'const char**' to 'char**' [-fpermissive]
    This is how the code looks:
    Qt Code:
    1. int main( int argc, const char** argv)
    2. {
    3. QCoreApplication a(argc, argv);
    4. serial.setPortName("/dev/usb0"); // The other configuration parameters follow...
    To copy to clipboard, switch view to plain text mode 

    What can I do to solve the problem?
    The first line of the code should not be changed.
    In the background is a bigger programm written by another person.
    I just try to modify the opensource code for personal use.


    Thank you very much

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Instantiate the QApplication object first

    The correct prototype for the C++ main() is one of:
    Qt Code:
    1. int main(int argc, char *argv[]); // argv is not const
    2. int main(int argc, char **argv); // direct equivalent of first option
    3. int main(void);
    To copy to clipboard, switch view to plain text mode 
    The last option is not useful in most Qt programs because argc and argv are required by QCoreApplication. Notice that argv is not const and, indeed, Qt may modify it in the QApplication initialisation.

    Provide a complete program that reproduces the first problem. This:
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSerialPort>
    3.  
    4. int main(int argc, char **argv)
    5. {
    6. QCoreApplication app(argc, argv);
    7. QSerialPort serial;
    8. serial.setPortName("/dev/ttyS0");
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 
    works just fine here (Qt5.2.0 Linux). Your code does not show the declaration of "serial". If it is global and static then the problem could simply be that it is initialised (and fails) before main() is run.

  3. #3
    Join Date
    Mar 2014
    Posts
    18
    Qt products
    Qt5

    Default Re: Instantiate the QApplication object first

    Thank you very much :-)

    In the original code a command line parser was used.
    As I do not really need this feature at the moment I removed it from the code.

    The code below works if I delete the lines 72 to 79.

    Otherwise I get a new error:
    Cannot create a QWidget without QApplication
    This is how the code looks:

    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QSerialPort>
    3.  
    4. #include "opencv2/video/tracking.hpp"
    5. #include "opencv2/imgproc/imgproc.hpp"
    6. #include "opencv2/highgui/highgui.hpp"
    7.  
    8. #include <iostream>
    9. #include <ctype.h>
    10.  
    11. using namespace cv;
    12. using namespace std;
    13.  
    14. QSerialPort serial;
    15.  
    16.  
    17. Mat image;
    18.  
    19. bool backprojMode = false;
    20. bool selectObject = false;
    21. int trackObject = 0;
    22. bool showHist = true;
    23. Point origin;
    24. Rect selection;
    25. int vmin = 10, vmax = 256, smin = 30;
    26.  
    27. static void onMouse( int event, int x, int y, int, void* )
    28. {
    29. if( selectObject )
    30. {
    31. selection.x = MIN(x, origin.x);
    32. selection.y = MIN(y, origin.y);
    33. selection.width = std::abs(x - origin.x);
    34. selection.height = std::abs(y - origin.y);
    35.  
    36. selection &= Rect(0, 0, image.cols, image.rows);
    37. }
    38.  
    39. switch( event )
    40. {
    41. case CV_EVENT_LBUTTONDOWN:
    42. origin = Point(x,y);
    43. selection = Rect(x,y,0,0);
    44. selectObject = true;
    45. break;
    46. case CV_EVENT_LBUTTONUP:
    47. selectObject = false;
    48. if( selection.width > 0 && selection.height > 0 )
    49. trackObject = -1;
    50. break;
    51. }
    52. }
    53.  
    54. static void help()
    55. {
    56. cout << "\nThis is a demo that shows mean-shift based tracking\n"
    57. "You select a color objects such as your face and it tracks it.\n";
    58.  
    59.  
    60. cout << "\n\nHot keys: \n"
    61. "\tESC - quit the program\n"
    62. "\tc - stop the tracking\n"
    63. "\tb - switch to/from backprojection view\n"
    64. "\th - show/hide object histogram\n"
    65. "\tp - pause video\n"
    66. "To initialize tracking, select the object with mouse\n";
    67. }
    68.  
    69.  
    70. int main( int argc, char *argv[])
    71. {
    72. QCoreApplication app(argc, argv);
    73. serial.setPortName("/dev/rfcomm0");
    74. serial.open(QIODevice::WriteOnly);
    75. serial.setBaudRate(QSerialPort::Baud115200);
    76. serial.setDataBits(QSerialPort::Data8);
    77. serial.setParity(QSerialPort::NoParity);
    78. serial.setStopBits(QSerialPort::OneStop);
    79. serial.setFlowControl(QSerialPort::NoFlowControl);
    80.  
    81. help();
    82.  
    83. VideoCapture cap(0);
    84. Rect trackWindow;
    85. int hsize = 16;
    86. float hranges[] = {0,180};
    87. const float* phranges = hranges;
    88.  
    89.  
    90. if( !cap.isOpened() )
    91. {
    92. help();
    93. cout << "***Could not initialize capturing...***\n";
    94. return -1;
    95. }
    96.  
    97. namedWindow( "Histogram", 0 );
    98. namedWindow( "Demo", 0 );
    99. setMouseCallback( "Demo", onMouse, 0 );
    100. createTrackbar( "Vmin", "Demo", &vmin, 256, 0 );
    101. createTrackbar( "Vmax", "Demo", &vmax, 256, 0 );
    102. createTrackbar( "Smin", "Demo", &smin, 256, 0 );
    103.  
    104. Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj;
    105. bool paused = false;
    106.  
    107. for(;;)
    108. {
    109. if( !paused )
    110. {
    111. cap >> frame;
    112. if( frame.empty() )
    113. break;
    114. }
    115.  
    116. frame.copyTo(image);
    117.  
    118. if( !paused )
    119. {
    120. cvtColor(image, hsv, COLOR_BGR2HSV);
    121.  
    122. if( trackObject )
    123. {
    124. int _vmin = vmin, _vmax = vmax;
    125.  
    126. inRange(hsv, Scalar(0, smin, MIN(_vmin,_vmax)),
    127. Scalar(180, 256, MAX(_vmin, _vmax)), mask);
    128. int ch[] = {0, 0};
    129. hue.create(hsv.size(), hsv.depth());
    130. mixChannels(&hsv, 1, &hue, 1, ch, 1);
    131.  
    132. if( trackObject < 0 )
    133. {
    134. Mat roi(hue, selection), maskroi(mask, selection);
    135. calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
    136. normalize(hist, hist, 0, 255, CV_MINMAX);
    137.  
    138. trackWindow = selection;
    139. trackObject = 1;
    140.  
    141. histimg = Scalar::all(0);
    142. int binW = histimg.cols / hsize;
    143. Mat buf(1, hsize, CV_8UC3);
    144. for( int i = 0; i < hsize; i++ )
    145. buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
    146. cvtColor(buf, buf, CV_HSV2BGR);
    147.  
    148. for( int i = 0; i < hsize; i++ )
    149. {
    150. int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
    151. rectangle( histimg, Point(i*binW,histimg.rows),
    152. Point((i+1)*binW,histimg.rows - val),
    153. Scalar(buf.at<Vec3b>(i)), -1, 8 );
    154. }
    155. }
    156.  
    157. calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
    158. backproj &= mask;
    159. RotatedRect trackBox = CamShift(backproj, trackWindow,
    160. TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
    161. if( trackWindow.area() <= 1 )
    162. {
    163. int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + 5)/6;
    164. trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
    165. trackWindow.x + r, trackWindow.y + r) &
    166. Rect(0, 0, cols, rows);
    167. }
    168.  
    169.  
    170.  
    171. if( backprojMode )
    172. cvtColor( backproj, image, COLOR_GRAY2BGR );
    173. ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );
    174. serial.write("test/r/n"); //Here I want to use serial.write actions
    175. serial.flush();
    176. }
    177. }
    178. else if( trackObject < 0 )
    179. paused = false;
    180.  
    181. if( selectObject && selection.width > 0 && selection.height > 0 )
    182. {
    183. Mat roi(image, selection);
    184. bitwise_not(roi, roi);
    185. }
    186.  
    187. imshow( "Demo", image );
    188. imshow( "Histogram", histimg );
    189.  
    190. char c = (char)waitKey(10);
    191. if( c == 27 )
    192. break;
    193. switch(c)
    194. {
    195. case 'b':
    196. backprojMode = !backprojMode;
    197. break;
    198. case 'c':
    199. trackObject = 0;
    200. histimg = Scalar::all(0);
    201. break;
    202. case 'h':
    203. showHist = !showHist;
    204. if( !showHist )
    205. destroyWindow( "Histogram" );
    206. else
    207. namedWindow( "Histogram", 1 );
    208. break;
    209. case 'p':
    210. paused = !paused;
    211. break;
    212. default:
    213. ;
    214. }
    215. }
    216. serial.close();
    217. return app.exec();
    218.  
    219. }
    To copy to clipboard, switch view to plain text mode 

    And this is my .pro file:
    Qt Code:
    1. QT += core serialport
    2.  
    3. QT -= gui
    4.  
    5. TARGET = Demo
    6. CONFIG += console
    7. CONFIG -= app_bundle
    8.  
    9. TEMPLATE = app
    10.  
    11.  
    12. SOURCES += main.cpp
    13.  
    14. INCLUDEPATH += /usr/local/include/opencv
    15. INCLUDEPATH += /usr/local/include/opencv2
    16.  
    17. LIBS += -L/usr/local/lib \
    18. -lopencv_core \
    19. -lopencv_imgproc \
    20. -lopencv_highgui \
    21. -lopencv_ml \
    22. -lopencv_video \
    23. -lopencv_features2d \
    24. -lopencv_calib3d \
    25. -lopencv_objdetect \
    26. -lopencv_contrib \
    27. -lopencv_legacy \
    28. -lopencv_flann
    To copy to clipboard, switch view to plain text mode 


    What can I do to make it work?

    Thank you very much :-)


    Added after 1 16 minutes:


    I found the solution by myself :-)

    In the .pro file I added QT += widgets and in the main I included QApplication and replaced QCoreApplication by QApplication :-)
    Last edited by Brixus; 11th April 2014 at 14:01.

Similar Threads

  1. Replies: 5
    Last Post: 14th February 2012, 22:54
  2. Replies: 4
    Last Post: 8th April 2011, 07:47
  3. Cannot instantiate Plugin with UI
    By trallallero in forum Qt Programming
    Replies: 1
    Last Post: 30th October 2010, 10:34
  4. Replies: 21
    Last Post: 28th September 2010, 11:59
  5. Please instantiate the QApplication object first
    By zorro68 in forum Qt Programming
    Replies: 7
    Last Post: 25th October 2007, 13:20

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.