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. [SOLVED] How to initialize ui in the function?
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] How to initialize ui in the function?

Scheduled Pinned Locked Moved General and Desktop
11 Posts 4 Posters 8.4k 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.
  • K Offline
    K Offline
    kalster
    wrote on last edited by
    #1

    In my program, i have created a function. in that function is ui->textEdit->setText(totdMsg[totdNum]); which is crashing my program.
    error: tipoftheday.cpp:22: warning: 'ui' is used uninitialized in this function. Can I have help in this please. How to initialize ui in the function?

    when i replace Ui::TipOfTheDay *ui with Ui::TipOfTheDay *ui = new Ui::TipOfTheDay; then i do not get the warning message but my program still crashes.

    Tipoftheday.cpp
    @#include "tipoftheday.h"
    #include "ui_tipoftheday.h"
    #include "mainwindow.h"
    #include <QDesktopWidget>
    #include <QSettings>
    #include <QCheckBox>

    void DisplayTipOfTheDay();

    TipOfTheDay::TipOfTheDay(QWidget *Child) :
    QDialog(Child, Qt::CustomizeWindowHint |
    Qt::WindowCloseButtonHint ),
    ui(new Ui::TipOfTheDay)
    {
    ui->setupUi(this);
    DisplayTipOfTheDay();

    }

    void DisplayTipOfTheDay(){
    Ui::TipOfTheDay *ui;
    ui->textEdit->setText(totdMsg[totdNum]);
    }

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

    tipoftheday.h
    @#ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>

    namespace Ui {
    class TipOfTheDay;
    }

    class TipOfTheDay : public QDialog
    {
    Q_OBJECT

    public:
    explicit TipOfTheDay(QWidget *parent = 0);
    ~TipOfTheDay();

    private slots:

    private:
    Ui::TipOfTheDay *ui;

    };

    #endif // DIALOG_H@

    1 Reply Last reply
    0
    • K Offline
      K Offline
      kalster
      wrote on last edited by
      #2

      I made some changes to the header file, removing the Ui::TipOfTheDay *ui;, but that only disabled the creation of widgets functions from within the cpp file. I really need some help in this topic.

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        [quote author="kalster" date="1312774533"]
        Tipoftheday.cpp
        @
        void DisplayTipOfTheDay(){
        Ui::TipOfTheDay *ui;
        ui->textEdit->setText(totdMsg[totdNum]);
        }
        @
        [/quote]

        That part does not look healthy at all.
        You create the pointer, but how should the application know where it is pointing?

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        0
        • K Offline
          K Offline
          kalster
          wrote on last edited by
          #4

          I finally solved it. i created a new function by adding TipOfTheDay:: to the function name that was giving me the error. Now i do not need that pointer in the function. here is my code...

          tipoftheday.cpp
          @void TipOfTheDay::DisplayTipOfTheDay()@

          tipoftheday.h
          @public:
          void DisplayTipOfTheDay();@

          the above two codes work nicely. The program runs fine now.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            aamer4yu
            wrote on last edited by
            #5

            Your problem was in line 21 of tipoftheday.cpp
            You were declaring *ui locally. This local variable overrides the member variable visibility, and hence the error.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              kalster
              wrote on last edited by
              #6

              but i also declared it with the following code and my program still crashed but without the error message.

              @Ui::TipOfTheDay *ui = new Ui::TipOfTheDay@

              is this still declaring it locally. if so then what would be the alternative?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                aamer4yu
                wrote on last edited by
                #7

                You need not declare it locally. You have made it a member variable, remember ?
                (tipoftheday.h line 22).

                You just need to get rid of the line 21 in tipoftheday.cpp and rest should work fine.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kalster
                  wrote on last edited by
                  #8

                  if i removed line 21 from the cpp then i would get an error message error: 'ui' was not declared in this scope. line 22 in the header was declared as private and therefore i would need to declare it for the function.

                  create a new qt gui application and put the following into the mainwindow.cpp file and compile it. you will see the error that i am talking about.

                  @void test();
                  void test(){
                  ui->textEdit->setText("test");
                  }@

                  anyone know how to get this function to work?

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mlong
                    wrote on last edited by
                    #9

                    [quote author="kalster" date="1312838955"]
                    create a new qt gui application and put the following into the mainwindow.cpp file and compile it. you will see the error that i am talking about.

                    @void test();
                    void test(){
                    ui->textEdit->setText("test");
                    }@

                    anyone know how to get this function to work?[/quote]

                    Make it MainWindow::test()

                    When you create a function outside the scope of the class (even if it lives in the class's .cpp or .h file), it cannot see the ui variable.

                    Software Engineer
                    My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      kalster
                      wrote on last edited by
                      #10

                      yes, in the scope is what i did in my third post. I guess in the scope is the only way. thank you mlong

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mlong
                        wrote on last edited by
                        #11

                        No problem! There aren't really many reasons to have functions outside your class for this sort of thing. So, keeping them in scope is easy in that sense!

                        Please be sure and add a [Solved] to the beginning of the title of the thread if you feel like the problem is solved.

                        Software Engineer
                        My views and opinions do not necessarily reflect those of anyone -- living or dead, real or fictional -- in this universe or any other similar multiverse node. Void where prohibited. Your mileage may vary. Caveat emptor.

                        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