Results 1 to 10 of 10

Thread: Qt/C++ basic Q-s

  1. #1
    Join Date
    Jan 2006
    Location
    11 N 78 E
    Posts
    110
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Red face Qt/C++ basic Q-s

    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?
    Penguin #395953 using Qt for open-source development on X11 using C++ and
    Python via PyQt

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    85
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qt/C++ basic Q-s

    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.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt/C++ basic Q-s

    If you want to find a good book about C++ read this.

    Also check our links section.

  4. #4
    Join Date
    Jan 2006
    Location
    Socorro, NM, USA
    Posts
    29
    Thanked 3 Times in 3 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt/C++ basic Q-s


  5. #5
    piotrpsz Guest

    Default Re: Qt/C++ basic Q-s

    [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

  6. #6
    Join Date
    Jan 2006
    Location
    Lincoln, NE USA
    Posts
    177
    Thanks
    3
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt/C++ basic Q-s

    Quote Originally Posted by jamadagni
    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.
    Qt Code:
    1. void homestead::GenerateXTabAllCounties(){
    2. //////////// Cntyname vs MSHEC for all counties ///////////////
    3. QString sQuestion;
    4. sQuestion.append("Do you want to generate an All Counties\n");
    5. sQuestion.append("cross tab report for Homestead?\n");
    6. int reply = QMessageBox::question(this, tr("QMessageBox::showQuestion()"),
    7. sQuestion, QMessageBox::Yes, QMessageBox::No);
    8. if (reply == QMessageBox::Yes){
    9. ui.leStatus->setText("Preparing XTab Report...");
    10. QString queryStr = "SELECT cntyname, M1,M2,M3,M4,S1,S2,S3,S4,VA,";
    11. queryStr.append("(SUM(M1)+SUM(M2)+SUM(M3)+SUM(M4)+SUM(S1)+SUM(S2)+SUM(S3)+SUM(S4)+SUM(VA)) AS Totals ");
    12. queryStr.append("FROM(SELECT cntyname, ");
    13. queryStr.append("SUM(CASE WHEN offuse = 'M-1' THEN 1 ELSE 0 END)AS M1,");
    14. queryStr.append("SUM(CASE WHEN offuse = 'M-2' THEN 1 ELSE 0 END)AS M2,");
    15. queryStr.append("SUM(CASE WHEN offuse = 'M-3' THEN 1 ELSE 0 END)AS M3,");
    16. queryStr.append("SUM(CASE WHEN offuse = 'M-4' THEN 1 ELSE 0 END)AS M4,");
    17. queryStr.append("SUM(CASE WHEN offuse = 'S-1' THEN 1 ELSE 0 END)AS S1,");
    18. queryStr.append("SUM(CASE WHEN offuse = 'S-2' THEN 1 ELSE 0 END)AS S2,");
    19. queryStr.append("SUM(CASE WHEN offuse = 'S-3' THEN 1 ELSE 0 END)AS S3,");
    20. queryStr.append("SUM(CASE WHEN offuse = 'S-4' THEN 1 ELSE 0 END)AS S4,");
    21. queryStr.append("SUM(CASE WHEN offuse = 'VA' THEN 1 ELSE 0 END)AS VA ");
    22. queryStr.append("FROM hap");
    23. queryStr.append(this->dbYear);
    24. queryStr.append(".property_");
    25. queryStr.append(" GROUP BY cntyname ORDER BY cntyname)");
    26. queryStr.append("GROUP BY cntyname,M1,M2,M3,M4,S1,S2,S3,S4,VA ORDER BY cntyname");
    27. QSqlQueryModel *XtabModel = NEW QSqlQueryModel;
    28. XtabModel->setQuery(queryStr);
    29. if (XtabModel->rowCount() > 0){
    30. ui.leStatus->setText("Processing XTab rows ...");
    31. // generate html code for MS (offuse) versus Percent Exmption for all counties
    32. int nRecs = XtabModel->rowCount();
    33. int totM1 = 0;
    34. int totM2 = 0;
    35. int totM3 = 0;
    36. int totM4 = 0;
    37. int totS1 = 0;
    38. int totS2 = 0;
    39. int totS3 = 0;
    40. int totS4 = 0;
    41. int totVA = 0;
    42. int totTotals = 0;
    43. QDate dateToday = QDate::currentDate();
    44. QString XTabName = "D_";
    45. XTabName.append(dateToday.toString("MMdd"));
    46. XTabName.append("_XTab_CntyName_vs_MSHEC");
    47. XTabName.append(".HTML");
    48. XTabName.prepend("reports/");
    49. QFile file(XTabName);
    50. if (!file.open(QIODevice::WriteOnly | QIODevice::Text)){
    51. ui.leStatus->setText("Can't open XTab_CntyName_vs_MSHEC html file for output");
    52. return;
    53. }
    54. QTextStream out(&file);
    55. //mod the lines below to change code to batches between dates.
    56. out << homestead::HTML_Header1 ;
    57. out << "Homestead " << this->dbYear << " Crosstab Report for All Counties ";
    58. out << homestead::HTML_Header2 << "\n";
    59. out << "<TABLE width='100%' align=center><TR><TH>Homestead " ;
    60. out << this->dbYear << " Crosstab Report for All Counties " ;
    61. out << "</TH></TR></TABLE><P></P>" << "\n";
    62. out << "<TABLE width='100%' ><B><TR>";
    63. out << "<TH align=left width='20%'>County Name</TH>";
    64. out << "<TH width='8%' align=right>M1</TH>";
    65. ...
    66. delete *XtabModel
    67. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt/C++ basic Q-s

    Quote Originally Posted by piotrpsz
    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!
    Current Qt projects : QCodeEdit, RotiDeCode

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt/C++ basic Q-s

    Quote Originally Posted by fullmetalcoder
    how could you use the singleton pattern without pointers?
    Like this?
    Qt Code:
    1. class SingletonWithoutPointers
    2. {
    3. public:
    4. static SingletonWithoutPointers& instance() {
    5. static SingletonWithoutPointers instance;
    6. return instance;
    7. }
    8. // ...
    9. private:
    10. SingletonWithoutPointers() { /* ... */ }
    11. };
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Jan 2006
    Location
    Athens - Greece
    Posts
    219
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt/C++ basic Q-s

    Quote Originally Posted by fullmetalcoder
    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.
    Quote Originally Posted by fullmetalcoder
    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.

  10. #10
    Join Date
    Jan 2006
    Location
    New Zealand
    Posts
    10
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Qt/C++ basic Q-s

    Quote Originally Posted by jamadagni
    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()


    Quote Originally Posted by jamadagni
    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 =)


    Quote Originally Posted by jamadagni
    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.
    Last edited by Weaver; 10th February 2006 at 10:04.

Similar Threads

  1. Need immediate help on basic cpp
    By Sandip in forum General Programming
    Replies: 10
    Last Post: 21st September 2008, 11:33
  2. Where can I get basic WebKit software?
    By crazymoonboy in forum General Discussion
    Replies: 1
    Last Post: 4th September 2008, 15:15
  3. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 16:11
  4. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 18:14
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 16:09

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.