PDA

View Full Version : C++ runtime Error in Running Program



ramin.lich
3rd September 2014, 20:59
hi all
this is Full Source of My Program

#include "mainwindow.h"
#include <QApplication>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QAction>
#include <QFileDialog>
#include <QMessageBox>
#include <thread>
#include <fstream>
#include <QTextStream>
#include <time.h>
#include <windows.h>
using namespace std;



const tm* timeinfos;
void tray();
void refresh();
void starts();
void wait();
ofstream dif("d:\\dif.txt");
const char* const path = "d:\\dif.txt" ;
void starts();
int hour=0,minu=0;
ofstream times("d:\\time.txt");
struct tm * timeinfo;
time_t rawtime = time(0) ;
int sec,secd;
MainWindow w;



int main(int argc, char *argv[])
{
QApplication a(argc, argv);
w.hide();
std::thread first(tray);
std::thread second(starts);
std::thread third(refresh);
starts();
first.join();
third.join();

return a.exec();
}


void tray(){
QSystemTrayIcon *trayIcon=new QSystemTrayIcon();
QMenu *menu=new QMenu("TimeUsage");
menu->addAction("Open");
menu->addAction("Exit");
trayIcon->setContextMenu(menu);
trayIcon->setIcon(QIcon("e:\\icon.png"));
trayIcon->show();
trayIcon->showMessage("TimeUsage","TimeUsage Is Here!");

QObject::connect(menu,&QMenu::triggered,[&w](QAction* act){

const auto& text = act->text();
if(text=="Exit"){
QApplication::exit();
}
else if(text=="Open"){
w.show();
}


});
}


void refresh(){
for(;;){
wait();
time_t now = time(0) ;
ifstream dif(path);
time_t earlier = 0 ;
dif >> earlier ;

double secs = difftime( now, earlier );
minu=secs/60;
while(minu>60){
minu-=60;
hour++;
}
QFile times("d:\\time.txt");
times.open(QIODevice::Append);
QTextStream stream(&times);
stream<<asctime(timeinfos)<<endl<<hour<<"/"<<minu<<endl;
}
}


void starts(){
timeinfos = localtime ( &rawtime );

ofstream dif(path);
dif << rawtime << '\n' ;
dif.close();
refresh();
}


void wait(){
int i;
for (i=0; i<=10; ++i) {
this_thread::sleep_for (chrono::seconds(1));
}
}

the Program Has No Problem With Compiling But will Not Run And I Get the Following Error:
10612
Any Help Will Be Great

faldzip
3rd September 2014, 21:48
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).

rezas1000
4th September 2014, 09:31
this code is completely wrong. So what is the difference between Library of the console and The library of the desktop?