-3

I am a newbie to Qt and also have limited knowledge of C++. I am learning Qt using C++ GUI Programming with Qt4 and have some doubt on the Qt codes in creating a dialog. Here as follows is a function of flicked():

void FindDialog::findClicked()
{
    QString text = lineEdit->text();
    Qt::CaseSensitivity cs =
            caseCheckBox->isChecked() ? Qt::CaseSensitive
                                      : Qt::CaseInsensitive;
    if (backwardCheckBox->isChecked()) {
        emit findPrevious(text, cs);
    } else {
        emit findNext(text, cs);
    }
}

The source of the code is at finddialog.cpp I don't understand the meaning of the two operators ? and : above. Anyone can explain to me what are they used for?

jwm
  • 4,832
  • 10
  • 46
  • 78
  • 4
    Possible duplicate of [shorthand c++ if else statement](https://stackoverflow.com/questions/24793916/shorthand-c-if-else-statement) – eyllanesc Feb 12 '18 at 15:05
  • 3
    `if (a == true) b(); else c();` can be written like `a == true ? b() : c();` – vahancho Feb 12 '18 at 15:11
  • 2
    OT: Why would you learn Qt4 in 2018? The last version of Qt4 is 7 years old. – Jaa-c Feb 12 '18 at 15:12
  • @ I have textbook Qt4 in hand. Do you have any good suggestion to learning Qt5. – jwm Feb 12 '18 at 15:14
  • http://wiki.qt.io/Books – eyllanesc Feb 12 '18 at 15:15
  • @eyllanesc: thanks! I have also found a similar question here https://stackoverflow.com/questions/795286/what-does-do-in-c – jwm Feb 12 '18 at 15:17
  • 1
    @jingweimo: They have a [very good tutorials](http://doc.qt.io/qt-5/gettingstarted.html) on their wesite and also many [basic examples](http://doc.qt.io/qt-5/qtexamplesandtutorials.html). – Jaa-c Feb 12 '18 at 15:17
  • I recommend you take some time and do a search in SO since these types of questions have been answered multiple times, or learn C ++ first, that has nothing to do with Qt, but it is basic C ++. – eyllanesc Feb 12 '18 at 15:19
  • https://qmlbook.github.io/ – talamaki Feb 12 '18 at 17:28
  • Qt4 is so close to Qt5, that learning it will not be counterproductive, if you have the book. Qt5 and modern C++ add very nice things, and better ways to do a lot of stuff, so once you move on to them, keep an open mind and actively seek out the improvements and changes and learn them. But Qt4 is perhaps simpler, so might be easier to get started with (and is not wasted effort). – hyde Feb 13 '18 at 10:35

1 Answers1

1
foo ? bar : baz; //With foo evaluating to true or false

is no Qt specific feature. It is a standard C++ feature: the Ternary or Conditional Operator.

CharonX
  • 2,130
  • 11
  • 33