PDA

View Full Version : Two probably simple Qt4 Questions



frenk_castle
16th September 2009, 11:35
I have been studying the SDI/MDI example in my Qt4 and in my book Foundations of Qt development. I have two questions. I tried to provide enough details. If I wrote to much I apologize.

1.

In the Qt4 example there is a line of code



newAct->setShortcuts(QKeySequence::New);


The line with same functionality in the book is



newAction->setShortcut( tr("Ctrl+N") );


I have read the help provided with the Qt there is a whole table of values for enum QKeySequence::StandardKey. As far as I have managed to conclude the first version


QKeySequence::QKeySequence ( StandardKey key )

is platform dependent. It doesn't say, as far as I can see if second version


QKeySequence::QKeySequence ( const QString & key )

is platform dependent.

Is there any real difference between these to versions and if there are some what are they?

2.

There was a thread about this but it didn't clarify it for me at all.

If I have type class B for instance and I want to use it as a member in my type class A. Code that I would use would look something like this.

class_B.h


#ifndef CLASS_B_H
#define CLASS_B_H

class B
{
//some code
}

#endif


class_B.cpp


#include "class_B.h"
//some code


class_A.h


#ifndef CLASS_A_H
#define CLASS_A_H

#include "class_B.h"

class A
{
private:
B b;
//some code
}

#endif


In the example in Qt class MainWindow derived from QMainWindow has QAction class member among others. But instead of including the QAction header the code is



QT_BEGIN_NAMESPACE
class QAction;
class QMenu;
class QTextEdit;
QT_END_NAMESPACE


Will somebody explain to me what this code do exactly or where can I read it in details. I failed to find it in the Qt help. Why not just include all the headers? I know that namespace for instance can be a problem. When using for instance string from Standard template library even when you include the string header you need to write using std::string or to declare every string object with std:string sBuffer so that compiler can use the right type.

I would appreciate any help you can provide. Thanks in advance.

Lykurg
16th September 2009, 11:50
Is there any real difference between these to versions and if there are some what are they?

Yes there is a difference.

newAction->setShortcut( tr("Ctrl+N") );
is "hard coded" (not really platform dependent) which means that the action is triggered everywhere only by Ctrl+N.


newAct->setShortcuts(QKeySequence::New);
uses a platform depended short cut to which the users is used to. So that is the better choice for programming for multiple targets because the user can use his familiar short cuts.

But if you only look for windows there is no difference because QKeySequence::New is expanded to Ctrl+N...
EDIT: and also for all other platforms... (which are known today:p). But anyway this is only a special case for a new file. other "actions" like MoveToEndOfLine are different on Win and Mac...

frenk_castle
16th September 2009, 14:57
Thank you Lykurg. You explained it much better that Qt help. :) Especially because I plan to write cross-platform code.

If somebody could answer the second question or direct me where to look/read. I tried to search the net and help but came up empty. I could just copy the code but I hate copying code which I don't know exactly what it does. When error occurs and they always do it is hard to fix it with "borrowed" code.

Lykurg
16th September 2009, 15:37
for the
class XYZ;
in a header file, look for various sites about "forward declaration". it can save you some compilation time and you should normally prefer this. Right now I don't know how the macros QT_BEGIN_NAMESPACE and QT_END_NAMESPACE are expanded, but you don't need them in your personal code. So just skip that macros.