1 Attachment(s)
C++ runtime Error in Running Program
hi all
this is Full Source of My Program
Code:
#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[])
{
w.hide();
std::thread first(tray);
std::thread second(starts);
std::thread third(refresh);
starts();
first.join();
third.join();
return a.exec();
}
void tray(){
menu->addAction("Open");
menu->addAction("Exit");
trayIcon->setContextMenu(menu);
trayIcon
->setIcon
(QIcon("e:\\icon.png"));
trayIcon->show();
trayIcon->showMessage("TimeUsage","TimeUsage Is Here!");
const auto& text = act->text();
if(text=="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");
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:
Attachment 10612
Any Help Will Be Great
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).
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?