Qt quick or Qt widgets?



  • Hello all, I am new to the whole Qt thing.

    So far, I can't tell the difference between a Qt application and a Qt widget, and I can't figure out if I am supposed to make a Qt quick thing in Javascript, or a classic c++ widget. What is the official method for a making a simple program? I am trying to make a simple window that has a button I can press, and I would like it to have the appearance of a system window.


  • Moderators

    [quote author="nickw1881" date="1384846402"]
    So far, I can't tell the difference between a Qt application and a Qt widget,
    [/quote]
    a Qt Application is just an application which is written with Qt ;)
    So either QML or QWidgets. So the widgets or QML elements (or even both mixed) make up the application.

    [quote author="nickw1881" date="1384846402"]
    and I can't figure out if I am supposed to make a Qt quick thing in Javascript, or a classic c++ widget. What is the official method for a making a simple program?
    [/quote]
    It depends...
    I personally would suggest to start with the C++/widgets first, because you are closer to the actual Qt code and it helps you understand the basics, workflow and mechanics of Qt better.
    But you can also start with QML if you like.

    [quote author="nickw1881" date="1384846402"]
    I am trying to make a simple window that has a button I can press, and I would like it to have the appearance of a system window.[/quote]
    Such a simple example can be achieved with C++/QtWidgets and QML very easily. But as i said since you are new to Qt i would recommend to start with the C++/QWidgets part first and once you feel comfortable with Qt and understand the basics you can explorer the QML world.



  • Ok, c++ widgets it is! It's been 3 weeks of pretty constant head banging so far with ZERO results :(

    What I am trying to do is to create a widget window, a button, a function (which will run some C code that I've already written), and link the button to the function. If I can figure that out I can probably make more buttons and add line-edits and all kinds of things.

    Where are the instructions on how to do that? The examples go from too basic to be useful to too complex really quick, and I can't make sense of the more complex ones that have multiple elements.


  • Moderators

    you should read some "tutorials":http://qt-project.org/doc/qt-5.0/qtdoc/gettingstarted.html or get a "book":http://qt-project.org/books this will ease your life alot ;)
    If you decide to get a book i recommend "C++ GUI Programming with Qt 4 (2nd Edition) - The official C++/Qt book" or "Foundations of Qt Development"

    To your example (very minimal):
    @
    int main(int argc, char **argv)
    {
    QApplication app(argc, argv);
    MyWindow w;
    w.show();
    return app.exec();
    }
    @

    @
    class MyWindow : public QWidget
    {
    Q_OBJECT

    public:
    MyWindow(QWidget* parent = 0) : QWidget(parent)
    {
    QPushButton* button = new QPushButton("Click me", this);
    button->move(10,10);
    connect( button, SIGNAL(pressed()), this, SLOT(onButtonPressed()));
    }

    protected slots:
    void onButtonPressed()
    {
    //do whatever you want todo on button press
    }
    }
    @



  • I guess the tutorials are just hard for me to understand because I am not good with c++. Lines like this constructor code are very confusing to me:
    widget::widget(QWidget *parent):QWidget(parent), ui(new Ui::Widget)

    This is probably really crucial, here is how I read this:

    widget[namespace]::
    widget[constructor](QWidget[Expected argument] parent[pointer to the object in the argument]):
    QWidget[widget inherits all the stuff in a QWidget object](parent[argument needed in the inherited generic QWidget object]),
    ui[the object used to fill the 'parent' object argument in the QWidget object being inherited by the widget](new Ui::Widget
    [partition some memory space for the widget])

    am I right? I understand inheritance, but don't completely understand the purpose of the text after the comma after the inherited class. I'm guessing it has something to do with the inherited class argument.

    Also, how do I know if my ui elements are supposed to be public or private? What maps the buttons that you declare in the .h file and define in the constructor code to the buttons drawn in the Qt creator page?

    This is my checklist for creating buttons:

    1. Draw the Button
    2. Declare the button in the widget.h file in the private/public section
    3. Add this line to the widget class constructor QPushButton* yournewbutton = new QPushButton("Click me", this);
    4. Create a slot for the button to link to. protected slots: void buttonOnePressed(){}
    5. Connect one of the yournewbutton signals to the slotconnect( button, SIGNAL(pressed()), this,SLOT(buttonOnePressed()));

  • Moderators

    see comments in the code:
    @
    //namespace::method (in this case constructor)
    MyWindow::MyWindow(
    QWidget* parent //parameter
    )
    : //from here on memeber initialization
    QWidget(parent) //base class initialization (inheritance)
    , ui(new Ui::Widget) //initialization of the member variable "ui" with the value "new Ui::Widget"
    {
    //constructor body
    }
    @
    It's good practice and also recommended to do the member-initialization before entering the constructor's body. The "ui" varibale comes from Qt when using the designer for example.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.