Classes

Pages: 12
Hi everyone, I'm kinda introducing myself to the forum and going to ask a question too, I feel I may be in and out of here quite a bit, I am determined to crack this! XD

Let me just say, I am hopefully in here to chat (about code) as much as I am to ask specific questions, So there will be rambling... You are warned... lol

For the past 3 days I have been studying this video...

http://www.youtube.com/watch?v=-WwGMNGRHdw

It seems like a great resource for anyone else that is trying to learn C++ so thought I would pop it in there, if that is okay, if not please remove it at will.

I am kinda getting a small inkling as to how certain parts work, but the project I am dealing with is super complex (to a mere newb like me).

You could say, well do something easier first...

Sure I could and I have tried some simple projects and was delighted when they worked :)

But I also want to dive right in the deep end and get stuck in there! lol

So... this brings me to the question...

I have a program that is already in existence, and I want to modify it (I do this a lot with website templates etc)

More specifically I want to take a piece of data that is being displayed in a tooltip (I am using qt creator, the prog was written with it) and display it some where else. The tooltip in question is in itself populated by live data, not just something plopped on a qlabel in qt-designer, just to be clear.

So first attempt I thought aha, I will copy everything and paste it in to the new page where I want it and bingo! Nope... XD Turns out this info I want trails back about 8 pages lmao... By doing this (tracing back all the pages and copying and altering names slightly) I did manage to get some data to show in the place I wanted it, but it seemed like a bit of a crazy number, that would change only when you reloaded the application and sometimes it wouldn't change at all. When I say a crazy number, I mean a number I don't recognize to represent anything, certainly not what I wanted.

Then, by watching the video above I learned about classes (YAY we got there! the subject lol). I saw how I could call a class from another, this seemed a better approach than copying all the code XD. I learned also about class permissions, and I got to the point where the compiler tells me that the method I am trying to get to is in private! YAY! That was awesome for me as it meant that it was at least trying to talk to the right page and code section.

I am kinda used to doing things like this with css and javascript etc when mucking about with websites.

The problem I have now is how to access these private areas, I have added a "freind class" to the target page... but I am a little confused with how to use it.

It would also seem that the private areas I want to access use pointers (something I have not fully read up/watched about yet.

This is from the target header file.
1
2
3
4
5
6
7
8
   Q_OBJECT
public:
    explicit BitcoinGUI(QWidget *parent = 0);
    ~BitcoinGUI();
friend class OverviewPage;

private:
 QLabel *labelStakingIcon;


This is from the .cpp that wants to be able to use these private methods

1
2
3
4
5
6
 
int updateStakingIcon()
{
    return BitcoinGUI::labelStakingIcon();

}


Now label staking icon is a picture obviously so thought it would be a nice easy target in learning how to use these methods from within another class...

I don't know what I would do once that gets past the compiler, but cross that road when I get to it.

For those that read all my drivel, well done. :)

Please don't berate me for my long drawn out comment, I like to give a bit of background and besides I did warn you in the first few sentences. lol



labelStakingIcon is not an int or static or a function. I would suggest learning more about classes and c++ in general. Do you even know why labelStakingIcon is private?
Last edited on
1
2
3
4
5
int updateStakingIcon()
{
    return BitcoinGUI::labelStakingIcon();

}
It's hard to answer without seeing what labelstackingicon is. Also...From the looks the function is static?


I would however suggest a book called "C++ GUI Programming with Qt 4" by Jasmin Blanchette (I have the second edition and I don't think there is a Qt5). There are numerous PDF's of the book online. Here is one of them: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf


As for acessing private variables you are going to want to use some sort of getter.
1
2
3
4
type getSomething() const
{
    return something;
}
You may also find it helpful to look at the c++ tutorials on this website http://www.cplusplus.com/doc/tutorial/ , http://www.learncpp.com/ , and http://www.parashift.com/c++-faq/

Another thing to mention there is a forum dedicated to Qt on QtCentre http://www.qtcentre.org/forum.php it may be more helpful than this site if you are using pure Qt
There's a Qt 5 but it didn't do much to change any C++ stuff, if I remember correctly.
Edit: Was just trying to confirm what I wrote, apparently Qt 5's "Qt Quick" did add some C++ classes.
Last edited on
Thanks for your replies guys, and you are right I do need to learn more... which is kinda why I am here XD

In the beginners section...

No, I do not even know why labelstakingicon is in the private section... XD

Of course I will continue reading and learning and thank you for the references giblit, I have read a fair few of the tutorials on this site and I am a member of the Qt creators forum, I joined here because I don't want to get stuck using Qt creator, I wanted to be around some pure C++ if you get me.

Also to note at one stage I do believe I got it to say that it is static, I obviously don't know the meaning of this...

To be fair this post was more for opening up a dialogue than getting my problem solved, I know it will take more than a quick, oh do this reply.

But it has already yielded great results with some fab info from giblit, please one and all post your best guides/resources/videos, I shall gobble them all up! XD

EDIT: Oh also, I did notice other sections using the getStakingIcon and things like that, I was trying all that the first method where I went through copy pasting everything XD
Last edited on
Well to note I do compile this with qt4, although I can compile it with qt5 too, I find qt4 easier to deal with. I was looking at qt quick, seems like that has a lot of nice potential.

I also mucked about with qt webkit for about a week trying to get it incorporated into the program, it worked but then broke some other aspects, it was most likely a missing dll or something.

webkit can't be compiled statically and the guide I use to compile the program uses a static build.

Of course I did build it dynamically and that is where I had the webkit working but I was getting runtime errors on first load and as I said some other unwanted side effects. XD

It was most likely a bad idea from the get go anyway from a security standpoint, but I am just experimenting really.
OP wrote:
No, I do not even know why labelstakingicon is in the private section... XD
Private members can only be accessed by other members and friends. http://www.cplusplus.com/doc/tutorial/classes/ (very top)

Also to note at one stage I do believe I got it to say that it is static, I obviously don't know the meaning of this...
http://www.cplusplus.com/doc/tutorial/templates/ (middle)

post your best guides/resources/videos
well for reference on c++the main two for me are http://en.cppreference.com/w/ and http://www.cplusplus.com/reference/ You will probably find yourself looking at these pages a lot to find new features and to get a refresher on some of the other ones. Though for you since you are using Qt there is also http://qt-project.org/doc/

I don't really watch c++ videos I find myself as more of a self-learner. I took a few classes at the university though and they were very slow paced and I was just teaching myself.

If you want any c++ books though @Duoas has a list on the FAQ (WIP) http://www.cplusplus.com/faq/beginners/books/

I would suggest getting a basic understand of OOP and then work with Qt. Also, note that in Qt they have an "Object tree memory management" probably another word for it. Basically though the parents delete all of their children so that is why you can use new without delete and not worry about memory leaks.
Last edited on
Yes as I said in the op I have used a friend class and so kinda know about that, I think the other chap was asking if I know WHY that particular method (is it even a method? XD) is in private as apposed to say public, and yeah, I don't know why that is...

I like a mix I think, the video above I find very good. I can watch it and I take notes as it goes, I can just pause and rewind and such, I have learned a fair bit from that already, but yeah I also like to read about it too. I like the fact you find self teaching better than uni... I am rather miffed as to why formal education still exists! XD

Again thank you for the books and links I will be sure to check them all out, just making a coffee and going to settle into the qt4 pdf :)
Already seeing that the qt4 pdf will be really awesome! It directed me to appendix d first (regarding my lack of C++ knowledge) So starting there and seeing already that it will be very helpful indeed, lots of bells ringing.

Thanks again, I will be back when I am done reading this erm, small pamphlet XD
I'm sorry if I came off as discouraging.

Yes, I was asking because you could just make it public. But, there are reasons for it being private, and these are the reasons I said you need to learn more.

I might say that learning Qt at the same time makes things a little tougher, but yes, to each his/her own.

This site is my best source of knowledge. Since I signed up, my real life programming has drifted to web-apps, but c++ is still my favorite. Basically, I go through the sections and look for people's homework problems that I can do for fun.

Hey LowestOne, it's cool, I know you get assholes like me coming by these places all the time that haven't a dafny XD

But I am trying, I am reading and watching all I can, I did try to display this data for 3 days straight, when I say days, I mean ALL day and night lol... But yeah you are dead right, I should defo learn more. I realize you could make it public, I think I even did try that, but I think it complained about something, sorry I can't be more specific, I think that could be when I saw the static comments/errors but can't be sure, I have mucked about with it a lot since so can't really get back to that point easily to tell for sure.

Anywho, I do really need to read and experiment a whole lot more first.

I read appendix d in the pdf, and it was awesome! As it reiterated and explained some things I had seen before, like in another way so made it clearer for me. I think just going over things again is great on it's own anyway.

I also noticed the bit about "extern" I have seen this in the code of the program and so looks like that is something I can use.

But I have downgraded for a while and I have been mucking about with the examples at the beginning of the pdf, it does make it easier to see what the certain components are doing, some is sinking in so that's good. I will probably give appendix d and few reads.

Yeah I see your point with Qt, as it does add a whole other level to the code and they do, do things differently it seems or rather they extend the library, would that be right? With their template classes and such, to be fair I think I may fine that easier, and I have had a good play with qt designer and starting to get my head round that.

I notice in the pdf that there are tools for looking at your code and finding memory leaks etc. I ran the program I want to play with through it. I used CPP, man I'm not sure how the program still runs judging by that! XD

Warnings and leaks all over the show.

But could this be because it doesn't recognize Qt's classes and such?

Anyway much to learn and I am excited to grow a little more knowledge everyday! :)

Thanks to everyone for your replies, I will let you know how I am getting on and sure I will be around and try to pick some of your brains when I can XD

Oh and thanks for not kicking me up the behind too much lol
Last edited on
I have one question actually, I see this...

1
2
QWidget *window = new QWidget;
window->setWindowTitle("Enter Your Age");


And it has the setWindowTitle, giblit eluded to using this kind of thing before, and I have seen it in the code...

Now, from what I understand, QWidget is a class and * is a pointer, and it is giving QWidget the alias of "window", is that correct? Or is it better to say that * is making "window" a pointer?

Now, I mucked around and changed window, to pineapple XD all the iterations of window and it ran just the same so I know that window could be anything, but setWindowTitle, is this something pre-defined in QWidget? because I tried to change that but couldn't.

I have seen things in the main project (that I ultimately want to work with) things like "setLabelStakingIcon" but this looks like something that they have made (as opposed to setWindowTitle that appears predefined), so I guess they are using a custom class? Or perhaps you can define your own "set" statements within a Q class?

Sorry for my bad use of terminology


Edit: Ahhh it's okay, I got to the bit regarding Qt assistant, and I see that they are indeed predefined, I can see that would be very handy info to have...

Though that still doesn't explain a custom one, though I'm sure it will be explained at some point.
Last edited on
Now, from what I understand, QWidget is a class and * is a pointer, and it is giving QWidget the alias of "window", is that correct? Or is it better to say that * is making "window" a pointer?
window would be the variable name (alias) for a type of a pointer to a QWidget the thing with QWidgets though is that even though you use new to create them, you do not need to use delete to deallocate it. QWidgets will delete all their children (and each child will delete any of theirs) then themselves.

but setWindowTitle, is this something pre-defined in QWidget? because I tried to change that but couldn't.
On line 2 window->setWindowTitle("Enter Your Age"); -> is used on pointers to access a member in other words setWindowTitle is a member function of the QWidget object being pointed to by window (window being the variable). http://www.cplusplus.com/doc/tutorial/classes/
So yes, it is pre-defined in QWidget it is a public member function(in this case a slot): http://qt-project.org/doc/qt-5/qwidget.html
http://qt-project.org/doc/qt-5/qwidget.html#windowTitle-prop It has a parameter of a QString (which will be the new title)

I have seen things in the main project (that I ultimately want to work with) things like "setLabelStakingIcon" but this looks like something that they have made (as opposed to setWindowTitle that appears predefined), so I guess they are using a custom class? Or perhaps you can define your own "set" statements within a Q class?
You can make any functions you want inside your class then call them accordingly. By the way the class you made is probably inherited from a QWidget so you should be able to use any public or protected members of QWidget inside of your class.
Yeah I am on the example where it deals more with slots just now, I was pulling my hair out too, as it didn't compile, it complained about "invalid use of incomplete type"

I scanned over to code making sure it was all correct, then checked to see which Qt creator I was using, it was 5.3.1 XD, so yeah switched it over to 4.8 and it compiled fine, so it would seem they made some fundamental changes in Qt5. Something I need to take note of I guess.

I think the hardest bit for me is getting my head round the terminology lol, but it will come the more I read about it.

Thanks for the info, I will try to digest it. Time for another coffee me thinks.

:)
Hmmm starting to wonder if this guide is too out of date or something, I'm doing one of the examples, and it just will not compile, I have checked and quadruple checked it is all present and correct

main.cpp

1
2
3
4
5
6
7
8
9
10
11
#include <QApplication>
#include "sortdialog.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    SortDialog *dialog = new SortDialog;
    dialog->setColumnRange('C', 'F');
    dialog->show();
    return app.exec();
}


sortdialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <QtGui>
#include "sortdialog.h"

SortDialog::SortDialog(QWidget *parent)
    : QDialog(parent)
{
    setupUi(this);

    secondaryGroupBox->hide();
    tertiaryGroupBox->hide();
    layout()->setSizeConstraint(QLayout::SetFixedSize);

    setColumnRange('A', 'Z');
}

void SortDialog::setColumnRange(QChar first, QChar last)
{
    primaryColumnCombo->clear();
    secondaryColumnCombo->clear();
    tertiaryColumnCombo->clear();

    secondaryColumnCombo->addItem(tr("None"));
    tertiaryColumnCombo->addItem(tr("None"));
    primaryColumnCombo->setMinimumSize(
                secondaryColumnCombo->sizeHint());

    QChar ch = first;
    while (ch <= last) {
        primaryColumnCombo->addItem(QString(ch));
        secondaryColumnCombo->addItem(QString(ch));
        tertiaryColumnCombo->addItem(QString(ch));
        ch = ch.unicode() + 1;
    }

}


sortdialog.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef SORTDIALOG_H
#define SORTDIALOG_H

#include <QDialog>

#include "ui_sortdialog.h"

class SortDialog : public QDialog, public Ui::SortDialog
{
    Q_OBJECT

public:
    SortDialog(QWidget *parent = 0);
    void setColumnRange(QChar first, QChar last);
};

#endif 




Now I just solved this (was trying as I wrote this post XD ) but I don't get why...

I changed class SortDialog : public QDialog, public Ui::SortDialog to class SortDialog : public QDialog, public Ui::sortDialog

And now it compiles... 0.o Anyone know what that is all about?

EDIT: Hmmm just noticed there is some odd behavior with the exe now, maybe I did something wrong with the ui page, must be that...

Going to go over that again.

EDIT: I did do the ui page slightly wrong, but fixed that, and it still works with sortDialog and not SortDialog, I must have been staring at the screen too long, I can't see why this is.

EDIT: haha It WAS my bloomin fault afterall, I had named the widget incorrectly (I think) I named it sortDialog but guess it should have been SortDialog XD
Last edited on
Its probably just you called the form that you made sortDialog rather than SortDialog. Remember, C++ is case sensitive, as in int a; is different to int A;.
Yeah you are right NT3, I didn't think to look on the Ui page at first. I am definitely seeing a little more how it works but I think I just need to keep practicing to solidify it.

Really enjoyable though, and not really AS daunting as I once thought.

I have a question about memory as I think that could be one of the more difficult concepts to grasp...

I kinda see how you can assign certain data a certain memory size, if I am understanding correctly. So for characters it is char and small numbers are int... Is that right? Then you get up to qint64? (Sorry in Qt it is called this but I know that is specific to Qt)

I notice on the program I want to work with (an others like it) that they can lock up quite a lot, when I say lock up, I mean say you make a particular action, the prog will go unresponsive for a time, it can vary, it definitely relates to size of the data being processed... Cos for instance there is a function on some of these progs called "staking" (doesn't really matter what that is if you don't know)... When you achieve stake a number is split in 2... They keep splitting and splitting... I personally had in the end 7000+ "blocks" of data... There is a like a checkbox form where you can select all these individual blocks and recombine them. Like in an e-mail client you can select the main checkbox, which will in turn check all other boxes...

If I check this one it took hours to become responsive again...

It can also happen again when you try to send "dust" lots of small blocks of data, combined now as 1 when you send.

So it doesn't seem 1 large block is a problem, but rather the amount of blocks of data it has to deal with.

Is there something that can be done about this type of unresponsiveness, I'm just asking off the cuff more than anything, not really anywhere near the optimization stage yet.
Last edited on
For different data sizes, yes, they do exist. Technically, though, the data sizes of the C++ variables aren't fixed, but most popular compilers provide a header <cstdint> which gives the same kind of integer types, going by int8_t, int16_t, int32_t and int64_t, as well as unsigned versions and a couple of other specializations.

As for the 'unresponsive' problem, the normal solution would be to use some kind of multithreaded approach. Qt provides a bunch of ways to do this, but the general concept is to start off a seperate thread processing the data, then go back to handling the UI's event loop (maybe with a progress bar receiving updates on the, well, progress), and receive a message from the worker thread that it has finished, and the data is available. However, multithreading is a highly complex topic, and you are probably best off staying away for quite a while.
Last edited on
Oh, hmmm, so for instance if a variable was assigned char and it became bigger it would get promoted to int? Or am I totally missing the point of char and int XD, highly likely lol

But hey I had some strange success XD, I did tell myself that I would study till tomorrow and then have a bash at getting this data where I want it... But I was learning so many things and I could sort of see the relation to the program I am working with, so just had to try again armed with my newly found semi-knowledge lol

I managed to get the data showing, but just not quite how I expected it to...

So, I went back to doubling up some up the code into the new page I want the data displayed, but this time I knew a little bit more about what all the pieces are.

But the new, replicated image, doesn't appear on the page like I want it, it actually opens a new Mainwindow (guess cos it is the child of mainwindow?)

Also this image has a timer, to refresh some data it holds, but instead of this new window refreshing, it creates a new window, still with the old one present, quite funny, like little baby windows being born XD

I am proud though, to even get that far, the hover works etc... On all the baby windows XD

Yeah I think I will leave multi-threading to my pal (he's off on his hols for 2 weeks), he's a coder and works as some systems administrator or something, he's more systems than coding but he knows a darn sight more than me! XD I'm trying to swat up to impress him when he gets back lol

But thanks it is great to know that there are solutions there.

EDIT: I know I haven't provided any code, it's okay, I'm still playing but if I get really stuck I will bring some for sure XD

EDIT: Solved the baby windows being born problem, but it is still in a window on it's own rather than where it should be. But getting there XD
Last edited on
Oh, hmmm, so for instance if a variable was assigned char and it became bigger it would get promoted to int? Or am I totally missing the point of char and int XD, highly likely lol
nope, that's when you get undefined behavior. What NT3 was mentioning is that the size of char and int are compiler dependant here is a table: http://www.cplusplus.com/doc/tutorial/variables/

Might I ask what program you are making while testing out features? I know when I was reading the book I bought (same as that pdf) it had a section on creating a spreadsheet so I modified it into a food log/nutrition tracker (calorie counter I guess?).

There are some sample projects you can make on Qt website (they are also in the Qt example program thing that you may have downloaded with designer/creator) http://qt-project.org/doc/qt-4.8/gettingstartedqt.html --notepad

http://qt-project.org/doc/qt-4.8/all-examples.html --list of all examples I think, the links below are some of the sub lists from this list.

http://qt-project.org/doc/qt-4.8/examples-dialogs.html --dialogs
http://qt-project.org/doc/qt-4.8/examples-draganddrop.html --drag and drop
http://qt-project.org/doc/qt-4.8/examples-itemviews.html --item view
http://qt-project.org/doc/qt-4.8/examples-layouts.html --layout
http://qt-project.org/doc/qt-4.8/examples-mainwindow.html --mainwindow
http://qt-project.org/doc/qt-4.8/examples-designer.html --designer
http://qt-project.org/doc/qt-4.8/examples-widgets.html --widgets

There are probably more out there.
Pages: 12