PDA

View Full Version : Qt Function doesn't work And there is no error



eyad
23rd February 2016, 10:17
The function ( QString getCreationDate() const ) doesn't return anything is it bugged or am i missing something obvious?

although there is another function that almost do the same thing but for another variable and it works like charm...
note* the compiler doesn't output any error. it just don't behave as expected!


//class counter (where the function is defined)
Counter( QString name,int start = 0,bool firstTime = true,QDate CreationTime = QDate::currentDate())
:
ClickTimes(start),
name(name),
firstTime(firstTime),
lastDate(QDate::currentDate()),
CreationTime(CreationTime)
{}

QString getDate() const
{
return lastDate.toString("dd/MM/yyyy"); //this one works perfectly
}

QString getCreationDate() const
{
return CreationTime.toString("dd/MM/yyyy"); //this one don't
}
/*---------------------------------------------------------*/
//MainWindow.cpp

//here is where the adding (creating the counter happens)
void MainWindow::addItemToList()
{
QString text2 = ui->inputBox_2->text();

//setting the model (some deleted code just to be on point)

//model properties (some deleted code just to be on point)

//adding to the model (some deleted code just to be on point)

//setting the counter
pCounters.push_back(Counter(text2,0,true));
ui->inputBox_2->clear();
}
/*---------------------------------------------------------*/
//this is where it get called

void MainWindow::SaveFile()
{
QFile Save("streak Counter.txt");
Save.open(QIODevice::WriteOnly|QIODevice::Text);

for(auto i = pCounters.begin();i != pCounters.end();i++)
{
QTextStream out(&Save);
if(i->isVirigin())
{
out << i->getName() << ',' << "virigin" << ',' << i->getCreationDate() << endl; //getCreationDate() doesn't work!

}
else
{
int counterNumber = i->getNumber();
out << i->getName() << ',' << QString::number(counterNumber)<< ',' << i->getDate() << ',' << i->getCreationDate() <<"\n";
}
}
Save.close();
}

//output file (example)

thing1,virigin,

or

thing2,3,22/2/2016,

i hope that someone may pass by and help me... as no one did In StackOverflow.. and i really can't figure out whats the problem.

anda_skoa
23rd February 2016, 10:44
Have you looked at the value of CreationTime inside that function?
Is it a valid QDate object?

E.g.


QString getCreationDate() const
{
qDebug() << Q_FUNC_INFO << CreationTime;
return CreationTime.toString("dd/MM/yyyy"); //this one don't
}

Or with the debugger.

Cheers,
_

eyad
23rd February 2016, 13:01
@anda_skoa

well i never tried the qdebug before, however when i did what you said, CreationTime doesn't have anything in it ( "" ) .

thats the QDebug message => class QString __cdecl Counter::getCreationDate(void) const QDate("")

so i changed this


pCounters.push_back(Counter(text2,0,true));

to


pCounters.push_back(Counter(text2,0,true,QDate::cu rrentDate())); //i thought this maybe the problem as Qt doesn't like my defaults

but still the variable don't get anything even though i explicitly passed the date to it.

anda_skoa
23rd February 2016, 13:36
Since your member and your constructor argument have the same name, have you tried different names?

Cheers,
_

eyad
23rd February 2016, 13:58
i tried this after reading you reply.
but it didn't change a thing.....

i may give you the whole solution if you want , its a small program after all (2 headers , 1 cpp , 1 ui) 56kb total
thats how desperate iam....

anda_skoa
23rd February 2016, 14:20
Attaching the program would definitely be useful.

Cheers,
_

eyad
23rd February 2016, 15:20
the program files part 1

the program files part 2

anda_skoa
23rd February 2016, 18:38
Your copy constructor doesn't copy CreationTime.

Cheers,
_

eyad
23rd February 2016, 19:02
yes sir, i know that cause i didn't use that constructor , not even once in the whole program, so it was going to be wasted time if i implemented it.

did this interfere with that function or its still don't work cause it don't feel like it :(

ars
23rd February 2016, 20:11
Hello,

from your code in addItemToList:

pCounters.push_back(Counter(text2,0,true,QDate::cu rrentDate()));
You create a Counter object and copy it into a vector, so it uses the copy CTOR. In function loadFile:

pCounters.push_back(Counter(Fields[0],0,true));
Again using the copy CTOR. Add some debug output into the copy CTOR and you will observe how often it is called.

You should add CONFIG+=c++11 to your .pro file. Moreover, get your CTOR initializer lists into the correct order to avoid warnings. Your counter class does not need any special handling when being copied. So remove your copy CTOR and let the compiler do the magic. It will copy all elements.

Best regards
ars

eyad
23rd February 2016, 20:21
oh, Thanks ars you did save the life of this Simple program.
and yea , good tip i will do this.

didn't imagine that the copy constructor is being used at all. well looks like i didn't go past the intermediate level afterall, but hey thats what they call "experience" , you gain it when you do your first hand craft :D

and thanks too anda_soka i think he wanted to say that the problem is in the copy constructor but i didn't get it (please, be more clear when talking to a newbie :D )

thanks guys for your help, i appreciate it :D
have a good day

oh, one quick question please (if you have time to answer this ofcourse)
where does the copy constructor is being used? cause i don't have a debugger so i don't know how to find such info :)

ars
23rd February 2016, 21:09
Hello,


where does the copy constructor is being used?
The copy CTOR is being used when the vector::push_back(x) method copies its argument x to the internal destination.

cause i don't have a debugger so i don't know how to find such info
Which development environment do you use? If you use mingw based environment there is always a debugger available: gdb. If you really cannot get a debugger for your development environment I suggest to switch to a different one (e.g. mingw). As soon as your program gets more complex you will need your debugger for tracing crashes or following your program execution step by step in case it does not do what you expected.

One more suggestion: Make your program compile without warnings. Compiling your current code throws warning messages which currently do not any harm to the software. But there will come the day when there is a severe warning that might crash your program execution and you don't see it because of tons of other warnings.

Best regards
ars

eyad
24th February 2016, 01:21
well, thanks for your reply.
i just use Qt Creator 5.5 64bit community edition.

at the point where you talk about mingw and the other "environments" well idk about this but i think the enviroment you are talking about are IDE-ish? if that is the case, nope i don't use any.

although i tried to use vs 2013 + qt addin 1.2.4 which gave me terrible times trying to make it work, where every solution literally failed.
so i use nothing, but qt creator itself which isn't that helpful (for obvious reasons....) -it even don't give me intelisense when i do something like i->(waiting for Counter.h functions) -

well about the vs problem its a story for another day, however i will take this opportunity to post the link to this problem description (http://stackoverflow.com/questions/35437850/qt-add-in-1-2-4-visual-studio-2013-bug) which i made in StackOverflow (http://stackoverflow.com/questions/35437850/qt-add-in-1-2-4-visual-studio-2013-bug)and as usual nobody helped me :(

maybe any of qt admins may see this and solve the problem....

anda_skoa
24th February 2016, 10:23
and thanks too anda_soka i think he wanted to say that the problem is in the copy constructor but i didn't get it (please, be more clear when talking to a newbie :D )

Well, the problem was that the copy constructor did not copy the member you were trying to access and that is what I wrote :)
The only other way I can come up with to write that is
"Your copy constructor needs to copy CreationTime"

Cheers,
_

ars
24th February 2016, 19:06
@eyad:

mingw is a port of Linux gcc to Windows (32, 64 bit). mingw itself does not come with any ide. However, a lot of Windows ides are integrating the mingw environment for compilation and debugging. Under Windows I'm using TDM MinGW 64 as compiler and Eclipse (and for some quick projects QtCreator) as ide. The same environment is used together with scripts for automating build processes. This is running on command line outside of any ide. So if you prefer working on command line mingw might be a good choice. Together with cygwin, msys or msys2 you can assemble a build environment under Windows where your scripts can easily be run under Linux too.

Best regards
ars

eyad
25th February 2016, 11:22
Wow, So Much info in just 3 lines :D
well i didn't know a thing about this before reading your Reply.
i will definitely research this, and ofcourse i appreciate your help so much guys :)