PDA

View Full Version : GUI Development Help



Atomic_Sheep
22nd June 2011, 14:20
Hi, I'm creating a GUI and I would love some guidance as programming isn't my strongest point.

I would inevitably like my GUI to work on Windows, Linux and Mac OS (this is why I decided to use QT as the way I understand it, it enables me to do this very thing).

As for the questions in regards to my GUI.

I started out by creating a new Qt Widget Project and selected Qt Gui Application.

I then went into the visual design page where I put together my GUI visually. However, I don't quite know which components to use for my intended purpose.

I'm basically trying to create this:

http://wallentins.com/images/CSL/FROM%20MCDU/Results/MCDU%20prepare%20Approach%201.jpg

So this widget will have a screen, it will have page select keys (e.g. DIR, PROG, PERF), it will have "line select keys" - that's those buttons next to the screen with -, a keyboard and a numberpad and finally keys enabling me to scroll through pages shown in the display of the corresponding Page (e.g. DIR, PROG etc).

The tricky part with all of this is that essentially, the "Page" keys select different pages, the keyboard and number pad enable the user to input data into the screen and the line select keys then copy the input data and paste them into the corresponding "lines" adjacent to the pressed line select key.

My problem is that I don't know which objects to use to create the layout for my GUI. I can create all the buttons of an appropriate size but then I don't know how I would link their as I explained, quite unique functionality to the screen part. Speaking of which, what would I use as the screen?

I was thinking of using something along the lines of the tab widget for the pages and then have a stacked widget assigned to each tab of the tab widget and then maybe use concealed radio buttons as the "action" which selects the required field to be used for data storage but I don't know whether this would work.

If I simply insert all the buttons that I require, then I can create the exact layout I need, however I don't what to use for the screen... if I use the second idea that I presented i.e. tab widget + stacked widget etc, then I don't know how to modify those widgets to the layout that I desire.

Any ideas on how I would tackle this problem?

nish
22nd June 2011, 14:57
You have to start with the Qt Tutorial found inside Qt Assistant before going into such a complex code.

Atomic_Sheep
22nd June 2011, 15:46
Hi, thanks, I just tried the "Creating Qt C++ Application" tutorial with the textFinder example and I more or less understood what was happening, however I'm not quite sure what Qt Assistant is. A search suggests that it should be in the Qt installation folder (On Windows, Qt Assistant is available as a menu option on the Qt menu.) but I don't see it there and a search of the whole computer has also come up with no results. Any ideas?

nish
22nd June 2011, 15:56
Qt assistant is just a app which shows html help of qt with some support for searching and indexing. If you have QtCreator then its already integrated in it. Or you can just jump to the online help at qt.nokia.com . Anyways, after u have read the tuturial, can you make a simple Calculator application without looking at the tutorial? If you can then only proceed with your original application.

Atomic_Sheep
22nd June 2011, 16:52
Awesome thanks. Another question though... what's wrong with just doing this whole project in Qt Designer and using Edit/Signals Slots? Or is this going to be a very inefficient process?

And a somewhat stupid question... when I add an object in Qt Designer, why does it not show the code for it in any of the .cpp files in the project viewer list? Only shows new objects/widgets getting created in xml view?

Atomic_Sheep
27th June 2011, 08:43
Hi, well I've been trying to create a calculator (haven't found any tutorials and not looking for one), have been trying to figure it out just by learning C++ theory. However I'm stuck...

I created a GUI... I made a button for each of the digits, and a button for each of the basic operators i.e. + - x /

After this, I added slots to all the buttons (Clicked()).

For the display I added the lcdNumber widget.

I'm not sure whether there is an easier way of doing it, but I created a class called values (2 files .h and .cpp). For the moment, I'm not sure how to simply attach a number to the back of another one, I'm guessing use string and then convert to integers in the function which gets called when the = key is pressed. Anyway, this class will house all the numbers needed e.g.

currentnumber - the number which is currently being created
firstnumber - after you press an operator key, the currentnumber which was being created gets assigned to firstnumber
secondnumber - after you press the equals key, the currentnumber gets assigned to secondnumber

I'll also need to add members to remember which operator was pressed

e.g. operator which will get a value of 1 2 3 4 depending on which operator gets called

so in the slot for the quals key:

void Calculator::on_pushButton_15_clicked() //this is the slot which gets called when the equals sign gets called
{

}

I'll be able to write code with a bunch of if statements, which will calculate

answer = firstvalue operator secondvalue

Somehow...

Q1.) How is my solution looking so far?

Q2.) I have no idea how to get the values to be shown in the lcdNumber widget? I'm not sure whether with the code that I've proposed, I'll need to create a connection or whether I'll be able to just add code for each button press slot which will directly affect the output of the lcdNumber widget. The latter is the way that I intended to do it but I'm unable to figure out how to get it to work. I'm not good enough at C++ to know enough functions and objects etc to know how to do this.

FelixB
27th June 2011, 08:47
I'm not good enough at C++ to know enough functions and objects etc to know how to do this.

then you should read some tutorials about plain c++ before starting with Qt...

Atomic_Sheep
27th June 2011, 08:52
I know how to do cin and cout and generic stuff which is taught in all tutorials but stuff to do with more graphical user interfaces I don't have any experience with and most tutorials just concentrate on the common themes presented in all C++ tutorials which I've read and watched plenty of. That's more what I was getting at.

EDIT: Can someone please explain what this code means?

#include <qlcdnumber.h>
#include <qslider.h>

#include "mywidget.h"

MyWidget::MyWidget( QWidget *parent, char *name ) : QVBox( parent, name )
{
QLCDNumber *lcd = new QLCDNumber( this );
QSlider *s = new QSlider( QSlider::Horizontal, this );

connect( s, SIGNAL(valueChanged(int)), lcd, SLOT(display(int)) );
}

All I know that it's a constuctor...

squidge
27th June 2011, 14:33
Which part are you struggling to understand?

Its a constructor as you say, and its using an initialiser list to call the inherited class QVBox constructor with the arguments parent & name.

Do you understand initialiser lists and inheritance ?

Atomic_Sheep
28th June 2011, 00:09
Started reading inheritance as you suggested, however, what I don't understand are the nuances of copy and direct initialization. I'm very familiar with copy initialization but why have direct and why have 2 different types? Is it even possible to do:

int a(7); ?
and is that the same as :
int a = 7;?

EDIT: I think I get where my confusion lies apart form not understanding other aspects of the afore mentioned code (so reading about those aspects right now)...

The thing that I don't understand is the coexistence of default and constructors which have parameters. I think this is an example of a constructor which has been called with specific values assigned to members...

I just need to practice compiling a bunch of classes in different scenarios and have a look at the results and convince myself that they are necessary. I just don't quite understand why we can't simply initialise the values of class members when we are actually writing code in the main.cpp section and are creating a variable of that class? I know we can have that but I don't understand why usually a default or a(more than one) parametrized constructor is included if we can always assign values later anyway or is this another fail safe mechanism used to simplify debugging?

nish
28th June 2011, 08:15
I don't understand why usually a default or a(more than one) parametrized constructor is included if we can always assign values later anyway or is this another fail safe mechanism used to simplify debugging?

Simple Answer:- To reduce your typing.
for example:-

class MyClass
{
private:
int val;
public:
MyClass() { /*empty*/ }
MyClass( int newVal ) { setVal(newVal); }
void setVal(int newVal) { val = newVal; }
int getVal() { return val; }
}

int main()
{
MyClass obj1;
obj1.setVal(5);

MyClass obj2(5);

}

as you see initializing obj1 requires two lines of code where as obj2 only requires 1 line. Consider the case where you have to create 50 objects. So you would be writing a lot of code and lot of bugs.

Complicated Answer:- It reduces code duplication, The object is assumed to be usable after its creation. You dont have to document the class like "Make sure u call setVal() at least once before calling getVal() otherwise the returned value is garbage.". And many others benefits.

Atomic_Sheep
28th June 2011, 12:26
Ok thanks, I had a feeling it was more of an implementation thing. Back to reading, I think I'm coming close to understanding.