Results 1 to 3 of 3

Thread: C++ runtime Error in Running Program

  1. #1
    Join Date
    Aug 2014
    Posts
    28
    Thanks
    15
    Qt products
    Qt5
    Platforms
    Windows

    Exclamation C++ runtime Error in Running Program

    hi all
    this is Full Source of My Program
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3. #include <QSystemTrayIcon>
    4. #include <QMenu>
    5. #include <QAction>
    6. #include <QFileDialog>
    7. #include <QMessageBox>
    8. #include <thread>
    9. #include <fstream>
    10. #include <QTextStream>
    11. #include <time.h>
    12. #include <windows.h>
    13. using namespace std;
    14.  
    15.  
    16.  
    17. const tm* timeinfos;
    18. void tray();
    19. void refresh();
    20. void starts();
    21. void wait();
    22. ofstream dif("d:\\dif.txt");
    23. const char* const path = "d:\\dif.txt" ;
    24. void starts();
    25. int hour=0,minu=0;
    26. ofstream times("d:\\time.txt");
    27. struct tm * timeinfo;
    28. time_t rawtime = time(0) ;
    29. int sec,secd;
    30. MainWindow w;
    31.  
    32.  
    33.  
    34. int main(int argc, char *argv[])
    35. {
    36. QApplication a(argc, argv);
    37. w.hide();
    38. std::thread first(tray);
    39. std::thread second(starts);
    40. std::thread third(refresh);
    41. starts();
    42. first.join();
    43. third.join();
    44.  
    45. return a.exec();
    46. }
    47.  
    48.  
    49. void tray(){
    50. QMenu *menu=new QMenu("TimeUsage");
    51. menu->addAction("Open");
    52. menu->addAction("Exit");
    53. trayIcon->setContextMenu(menu);
    54. trayIcon->setIcon(QIcon("e:\\icon.png"));
    55. trayIcon->show();
    56. trayIcon->showMessage("TimeUsage","TimeUsage Is Here!");
    57.  
    58. QObject::connect(menu,&QMenu::triggered,[&w](QAction* act){
    59.  
    60. const auto& text = act->text();
    61. if(text=="Exit"){
    62. QApplication::exit();
    63. }
    64. else if(text=="Open"){
    65. w.show();
    66. }
    67.  
    68.  
    69. });
    70. }
    71.  
    72.  
    73. void refresh(){
    74. for(;;){
    75. wait();
    76. time_t now = time(0) ;
    77. ifstream dif(path);
    78. time_t earlier = 0 ;
    79. dif >> earlier ;
    80.  
    81. double secs = difftime( now, earlier );
    82. minu=secs/60;
    83. while(minu>60){
    84. minu-=60;
    85. hour++;
    86. }
    87. QFile times("d:\\time.txt");
    88. times.open(QIODevice::Append);
    89. QTextStream stream(&times);
    90. stream<<asctime(timeinfos)<<endl<<hour<<"/"<<minu<<endl;
    91. }
    92. }
    93.  
    94.  
    95. void starts(){
    96. timeinfos = localtime ( &rawtime );
    97.  
    98. ofstream dif(path);
    99. dif << rawtime << '\n' ;
    100. dif.close();
    101. refresh();
    102. }
    103.  
    104.  
    105. void wait(){
    106. int i;
    107. for (i=0; i<=10; ++i) {
    108. this_thread::sleep_for (chrono::seconds(1));
    109. }
    110. }
    To copy to clipboard, switch view to plain text mode 
    the Program Has No Problem With Compiling But will Not Run And I Get the Following Error:
    Untitled.png
    Any Help Will Be Great

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: C++ runtime Error in Running Program

    1. You have to create QApplication before creating any widgets - if you must have this global variable MainWindow w; then make it a pointer and then allocate on a heap after QApplication creation.
    2. But it is not going to work either as you cannot access the widgets from threads other than UI thread (which is the main thread).
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  3. The following user says thank you to faldzip for this useful post:

    ramin.lich (4th September 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: C++ runtime Error in Running Program

    this code is completely wrong. So what is the difference between Library of the console and The library of the desktop?

Similar Threads

  1. Replies: 6
    Last Post: 3rd December 2013, 04:05
  2. Replies: 1
    Last Post: 25th September 2010, 08:20
  3. running external console program by gui program
    By alireza.mixedreality in forum Qt Programming
    Replies: 4
    Last Post: 24th April 2010, 18:05
  4. Program running error in qtopia 2.2.0
    By halloworld in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 15th February 2009, 09:23
  5. Replies: 1
    Last Post: 17th May 2006, 00:23

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.