Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Issue with QtWidgets (Complete beginner)



  • Hi, I'm a complete beginner with Qt. I just installed and already have some issues.
    I'm currently following this tutorial to create a Notepad small application: https://doc.qt.io/qt-5/qtwidgets-tutorials-notepad-example.html

    Everything works fine until I finish the "Designing the application" step.
    I have two issues:
    First one = in the Design section, I can't use "Layout" for my Text Edit widget. I can't understand why.
    The second issue = In the tutorial, it literally says that my Notepad header file should have multiple functions automatically "Spawned" by Qt, so it should look like this:

    #include <QMainWindow>
    
    namespace Ui {
    class Notepad;
    }
    
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit Notepad(QWidget *parent = nullptr);
        ~Notepad();
    
    private slots:
        void newDocument();
    
        void open();
    
        void save();
    
        void saveAs();
    
        void print();
    
        void exit();
    
        void copy();
    
        void cut();
    
        void paste();
    
    

    But instead, my header looks like this:

    #ifndef NOTEPAD_H
    #define NOTEPAD_H
    
    #include <QMainWindow>
    
    QT_BEGIN_NAMESPACE
    namespace Ui { class Notepad; }
    QT_END_NAMESPACE
    
    class Notepad : public QMainWindow
    {
        Q_OBJECT
    
    public:
        Notepad(QWidget *parent = nullptr);
        ~Notepad();
    
    private:
        Ui::Notepad *ui;
    };
    #endif // NOTEPAD_H
    
    

    Basically, the class in my header has 0 functions. Only constructor, and destructor.

    Thanks for the help in advance!


  • Moderators

    Hi, welcome to the forum.

    I can't use "Layout" for my Text Edit widget. I can't understand why.

    the tutorial has it slightly wrong. To lay out a widget in its parent select the parent (so click anywhere outside the text edit in this case) and then the lay out options are available.

    In the tutorial, it literally says that my Notepad header file should have multiple functions automatically "Spawned" by Qt, so it should look like this:

    Again it's a bit poorly worded in the context of the following code snippet. It says that "The wizard generated a header file for the Notepad class that has the necessary #includes, a constructor, a destructor, and the Ui object." and that part is true, but what it should say after that is that everything else not mentioned above is added as part of that exercise, so those functions you create yourself. The wizard generates a basic class like you posted and that is ok.


  • Moderators

    Hi, welcome to the forum.

    I can't use "Layout" for my Text Edit widget. I can't understand why.

    the tutorial has it slightly wrong. To lay out a widget in its parent select the parent (so click anywhere outside the text edit in this case) and then the lay out options are available.

    In the tutorial, it literally says that my Notepad header file should have multiple functions automatically "Spawned" by Qt, so it should look like this:

    Again it's a bit poorly worded in the context of the following code snippet. It says that "The wizard generated a header file for the Notepad class that has the necessary #includes, a constructor, a destructor, and the Ui object." and that part is true, but what it should say after that is that everything else not mentioned above is added as part of that exercise, so those functions you create yourself. The wizard generates a basic class like you posted and that is ok.



  • @Chris-Kawa I can't believe it was this simple. Thank you for the quick & informative answer, it helped a lot!


  • Lifetime Qt Champion

    Hi and welcome to the forums

    First one = in the Design section, I can't use "Layout" for my Text Edit widget. I can't understand why.

    Well first place the TextEdit on the form.
    Then right click any where on the form (not hitting the TextEdit)
    Then in the popup menu, goto Layout submenu and choose a layout there.

    alt text

    Yeah that example skips some steps and its not really clear.

    Those slots
    void open();
    void save();

    you should just paste them to your code. they wont auto spawn.

    We use them to in the connect statement for the buttons in the toolbar

    connect(ui->actionOpen, &QAction::triggered, this, &Notepad::open);
    connect(ui->actionSave, &QAction::triggered, this, &Notepad::save);

    ...

    ahh. to slow :)


  • Banned

    This post is deleted!

Log in to reply