Important: Please read the Qt Code of Conduct -

Functions in Qt

  • Hi all,

    As you all know, in C++ (and almost all other languages) we have functions that ease our tasks. For example here is a very simple function in C++:

    void print(int i) {
    cout << i ;

    Here, we send a parameter (say, print(3)), and the output is standard screen that the function prints into it the value 3.

    Now what is the equivalent of this function in Qt?
    Consider some box, widget, lineedit, for it to be the destination (output) instead of the standard screen.

  • Lifetime Qt Champion

    You can still use cout in a GUI application but it will of course not be shown in the GUI.
    So normally you do something like

    void mainwindow::print(int i) {
    ui->plainTextEdit->appendPlainText( QString::number(i) );

    Other way is to use

    and have all cout going to file. Then load this in application.

    So it mostly depends on what you are trying to do.

  • So why do you think Qt is a language like c++? Qt is a set of libraries written in c++.
    I think you have your paradigms mixed up:-)

  • @tomy ... To add to what @mrjj was talking about, there is form article
    that actually describes allowing std::cout to go to a text control. This can be useful if your UI needs a console, but for the most part if you are looking to debug use QtDebug. It is very nice. So, an example would be like:
    #include <QtDebug>
    qDebug () << "Wow! I got output";

    But yeah, think about Qt as an API or Toolkit that allows you to write great Apps in C++! Also think about anything you do in Qt from an object point of view and not a functional point of view. If you use C++ like an extension of C... you still get C.

  • @Buckwheat said in Functions in Qt:

    Also think about anything you do in Qt from an object point of view and not a functional point of view. If you use C++ like an extension of C... you still get C.

    Nice advice, I would recommend some good c++ books written by world class expert.

    The Definitive C++ Book Guide and List

    c++ support multiple paradigms, like traditional procedure programming(c like style), generic programming, oop, functional programming(lambda, std::function etc), stl, template meta programming. Each of them have their pros and cons, how to combine their power to create a good software is up to developer. No matter which style you pick, keep in mind do not prefer old school "c with classes", this style is same as "anti-pattern" in the world of modern c++(c++1/14/17).

  • Hi tomy,

    Consider below case:

    in UI(QML file)

    Text {
    text: ViewModel.progessValue /// text value will be notified from Qt cpp file and will display in text field.

    in Qt(.h file) /// ViewModel.h

    clas ViewModel
    Q_PROPERTY(QString progessValue READ progessValue NOTIFY progessValueChanged)
    int m_ProgValue;

    in Qt(.cpp file) /// ViewModel.cpp

    QString ViewModel::progessValue()
    QString str("%1");
    return str.arg(m_ProgValue);

    void ViewModel::execute() /// background thread function.
    for(int i = 0; i < MAX_RANGE; ++i)
    // Do operation.

    	emit progessValueChanged(); /// Notify the UI about the progress.


    Above case is an example for outputting values other than standard output. Hope this will make you know that significance of "function" (u said) in Qt way.

    Rajeesh Raveendran

Log in to reply