PDA

View Full Version : Qt/C++ basic Q-s



jamadagni
29th January 2006, 10:46
I am first reading C++ GUI programming in Qt to get a general idea of Qt and along the way I have got an idea of C++ specialties (over C which I know somewhat okay) of what a class is etc.

But I do not understand a few things:

Sometimes you have class.function() and sometimes class->function()

I don't understand the difference.

Also between & and *

void setEditText ( const QString & text )
void setValidator ( const QValidator * validator )

I have seen * going into the declaration of a function in C, but & -- ? But I note that & inside the argument occurs only for signals and slots. Something special here?

Last: I see the word const *after* the declaration of a function:

int currentIndex () const

What does this mean?

Codepoet
29th January 2006, 11:56
Read first a good introductory book about C++. There are so many new things you have to learn - even a new programming paradigm.
After you are comfortable with C++ you will be able to use Qt.

Which book? I have "The C++ Programming Language" by Bjarne Stroustrup. If you are fluent in C it could be a good book for you. Otherwise probably not.

jacek
29th January 2006, 13:45
If you want to find a good book about C++ read this (thishttp://www.qtcentre.org/forum/showthread.php?t=29).

Also check our links section.

Thomas
4th February 2006, 22:36
http://www.parashift.com/c++-faq-lite/index.html

Hth,
Thomas

piotrpsz
8th February 2006, 18:04
[QUOTE=jamadagni]
Sometimes you have class.function() and sometimes class->function()
I don't understand the difference.
[/CODE]

in first situation class is so called 'reference to class'.
In next one class is 'pointer to class'.

This is the same. But reference is better and saver.

best regards
piotr

GreyGeek
8th February 2006, 18:57
I am first reading C++ GUI programming in Qt to get a general idea of Qt and along the way I have got an idea of C++ specialties (over C which I know somewhat okay) of what a class is etc.

But I do not understand a few things:

Sometimes you have class.function() and sometimes class->function()

I don't understand the difference.

Also between & and *

void setEditText ( const QString & text )
void setValidator ( const QValidator * validator )

I have seen * going into the declaration of a function in C, but & -- ? But I note that & inside the argument occurs only for signals and slots. Something special here?

Last: I see the word const *after* the declaration of a function:

int currentIndex () const

What does this mean?

If you create a class on the heap with the "NEW" operator then you use "->" otherwise you are creating a class on the stack and you use the "." operator when referencing members. Here is some code which illustrates both methods.


void homestead::GenerateXTabAllCounties(){
//////////// Cntyname vs MSHEC for all counties ///////////////
QString sQuestion;
sQuestion.append("Do you want to generate an All Counties\n");
sQuestion.append("cross tab report for Homestead?\n");
int reply = QMessageBox::question(this, tr("QMessageBox::showQuestion()"),
sQuestion, QMessageBox::Yes, QMessageBox::No);
if (reply == QMessageBox::Yes){
ui.leStatus->setText("Preparing XTab Report...");
QString queryStr = "SELECT cntyname, M1,M2,M3,M4,S1,S2,S3,S4,VA,";
queryStr.append("(SUM(M1)+SUM(M2)+SUM(M3)+SUM(M4)+SUM(S1)+SUM(S2)+S UM(S3)+SUM(S4)+SUM(VA)) AS Totals ");
queryStr.append("FROM(SELECT cntyname, ");
queryStr.append("SUM(CASE WHEN offuse = 'M-1' THEN 1 ELSE 0 END)AS M1,");
queryStr.append("SUM(CASE WHEN offuse = 'M-2' THEN 1 ELSE 0 END)AS M2,");
queryStr.append("SUM(CASE WHEN offuse = 'M-3' THEN 1 ELSE 0 END)AS M3,");
queryStr.append("SUM(CASE WHEN offuse = 'M-4' THEN 1 ELSE 0 END)AS M4,");
queryStr.append("SUM(CASE WHEN offuse = 'S-1' THEN 1 ELSE 0 END)AS S1,");
queryStr.append("SUM(CASE WHEN offuse = 'S-2' THEN 1 ELSE 0 END)AS S2,");
queryStr.append("SUM(CASE WHEN offuse = 'S-3' THEN 1 ELSE 0 END)AS S3,");
queryStr.append("SUM(CASE WHEN offuse = 'S-4' THEN 1 ELSE 0 END)AS S4,");
queryStr.append("SUM(CASE WHEN offuse = 'VA' THEN 1 ELSE 0 END)AS VA ");
queryStr.append("FROM hap");
queryStr.append(this->dbYear);
queryStr.append(".property_");
queryStr.append(" GROUP BY cntyname ORDER BY cntyname)");
queryStr.append("GROUP BY cntyname,M1,M2,M3,M4,S1,S2,S3,S4,VA ORDER BY cntyname");
QSqlQueryModel *XtabModel = NEW QSqlQueryModel;
XtabModel->setQuery(queryStr);
if (XtabModel->rowCount() > 0){
ui.leStatus->setText("Processing XTab rows ...");
// generate html code for MS (offuse) versus Percent Exmption for all counties
int nRecs = XtabModel->rowCount();
int totM1 = 0;
int totM2 = 0;
int totM3 = 0;
int totM4 = 0;
int totS1 = 0;
int totS2 = 0;
int totS3 = 0;
int totS4 = 0;
int totVA = 0;
int totTotals = 0;
QDate dateToday = QDate::currentDate();
QString XTabName = "D_";
XTabName.append(dateToday.toString("MMdd"));
XTabName.append("_XTab_CntyName_vs_MSHEC");
XTabName.append(".HTML");
XTabName.prepend("reports/");
QFile file(XTabName);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
ui.leStatus->setText("Can't open XTab_CntyName_vs_MSHEC html file for output");
return;
}
QTextStream out(&file);
//mod the lines below to change code to batches between dates.
out << homestead::HTML_Header1 ;
out << "Homestead " << this->dbYear << " Crosstab Report for All Counties ";
out << homestead::HTML_Header2 << "\n";
out << "<TABLE width='100%' align=center><TR><TH>Homestead " ;
out << this->dbYear << " Crosstab Report for All Counties " ;
out << "</TH></TR></TABLE><P></P>" << "\n";
out << "<TABLE width='100%' ><B><TR>";
out << "<TH align=left width='20%'>County Name</TH>";
out << "<TH width='8%' align=right>M1</TH>";
...
delete *XtabModel
}

fullmetalcoder
9th February 2006, 17:00
in first situation class is so called 'reference to class'.
In next one class is 'pointer to class'.

This is the same. But reference is better and saver.

best regards
piotr

Reference is better? There are many cases where a pointer is MUCH better : how could you use the singleton pattern without pointers? Pointers are one of the greatests advantages of C++! References are nice but the impossibility of modifying them, make them safer undoubtlessly, but forbid the most advanced usages of pointers!

jacek
9th February 2006, 17:12
how could you use the singleton pattern without pointers?Like this?
class SingletonWithoutPointers
{
public:
static SingletonWithoutPointers& instance() {
static SingletonWithoutPointers instance;
return instance;
}
// ...
private:
SingletonWithoutPointers() { /* ... */ }
};

yop
9th February 2006, 17:25
Pointers are one of the greatests advantages of C++!Pointers have been around for quite a while longer ;) C++ didn't add or improve anything, they are still variables pointing to a memory location.
References are nice but the impossibility of modifying them, make them safer undoubtlessly, but forbid the most advanced usages of pointers!That's why you have the ability to use pointers and that's why C++ is great among other things. Use references when you can, and pointers when you have to. (http://www.parashift.com/c++-faq-lite/references.html#faq-8.6)

Weaver
10th February 2006, 09:01
Sometimes you have class.function() and sometimes class->function()

I don't understand the difference.

The former is for when "class" is an object. The latter is for when "class" is a pointer to an object.

class->function() is exactly the same as (*class).function()




void setEditText ( const QString & text )
void setValidator ( const QValidator * validator )

I have seen * going into the declaration of a function in C, but & -- ? But I note that & inside the argument occurs only for signals and slots. Something special here?

The first function is passing a QString by reference. The second is passing a pointer to a QValidator object.

You can think of a reference as an alias of an object (so you treat the reference as if it were the object), whereas a pointer hold the address of the object.

In practical terms a reference and a pointer are essentially the same thing. The difference is that a pointer can be NULL (ie point to nothing) but a reference must always refer to a valid object. And yeah, in C you can't pass by reference which is why you won't have seen it there.

I hope that makes sense =)



Last: I see the word const *after* the declaration of a function:

int currentIndex () const

What does this mean?

It means that your method wont (can't) modify the state of your object. ie it can't change the value of any member variables.

There is the cavet that if a member variable is declared "mutable" that the method can modify it regardless.