Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Newbie question about widgets
Qt 6.11 is out! See what's new in the release blog

Newbie question about widgets

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    santiagorf
    wrote on last edited by
    #1

    Hi all,
    Using the designer in Qt creator, I have created a widget with a QPushButton object on it called "pushButton", but I have two problems:

    1. How to access the objects pushButton
    2. How to add another object to the widget from the source code

    My code is the following

    main.cpp
    @
    #include <QApplication>
    #include "widget.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec&#40;&#41;;
    

    }
    @

    widget.h
    @
    #ifndef WIDGET_H
    #define WIDGET_H

    #include <QWidget>
    #include <QLabel>

    namespace Ui {
    class Widget;
    }

    class Widget : public QLabel
    {
    Q_OBJECT

    public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();
    QLabel * label;

    private:
    Ui::Widget *ui;
    };

    #endif // WIDGET_H
    @

    There is another file widget.ui that is the form with the QPushButton, and the file
    widget.cpp

    @
    #include "widget.h"
    #include "ui_widget.h"

    Widget::Widget(QWidget *parent) : QLabel (parent), ui(new Ui::Widget){
    // label =new QLabel("aaaaaaaaaa");
    //ui->pushButton->setText("test");
    ui->setupUi(this);
    }

    Widget::~Widget()
    {
    delete ui;
    }
    @

    where I have the two problems.

    1)In the first comment, I try to add a new label

    2)In the second comment, I try to change the text on the pushButton

    Neither of them seem to be the right approach.
    Any help would be much appreciated!

    1 Reply Last reply
    0
    • P Offline
      P Offline
      pkj__
      wrote on last edited by
      #2

      bq. In the first comment, I try to add a new label

      Label will be created by qmake generated code. Don't worry about it.

      bq. In the second comment, I try to change the text on the pushButton

      Do it after setupUI call.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        santiagorf
        wrote on last edited by
        #3

        thanks for the prompt reply. I

        I changed it to this; now I'm able to change the pushButton, but I still don't know how to add/display the label in the widget.

        @
        Widget::Widget(QWidget *parent) : QLabel (parent), ui(new Ui::Widget){
        ui->setupUi(this);
        ui->pushButton->setText("test");
        label =new QLabel("aaaaaaaaaa");

        }
        @

        1 Reply Last reply
        0
        • P Offline
          P Offline
          pkj__
          wrote on last edited by
          #4

          Open the ui file in qtcreator, drop a qlabel in the ui file.
          In the cpp file, @ui->label->setText("aaaaa");@
          You can add QLabel from code too, but I feel you might want to play around with adding items in widget.ui file at the moment. Also after compilation and linking, open the generated file ui_widget.h and see what all code is being generated for you.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            santiagorf
            wrote on last edited by
            #5

            I got the idea of adding items with widget.ui, and I also have been able to create create dialogues from code.
            However, I'd like to know how to add widgets from code into a form created using graphic tools.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved