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 make Ui read from a file and display it
Forum Updated to NodeBB v4.3 + New Features

[Solved] How to make Ui read from a file and display it

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 2.3k 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.
  • P Offline
    P Offline
    pragati
    wrote on last edited by
    #1

    Hi All,

    I am trying to fetch some data from a file and display it on a label or a say line edit

    Now if I try it in the following way that is create a label from code and then apply the logic it runs fine but if I try same logic on a ui made from a designer then it gives error.

    Code snippet which is working fine is:

    @

    #include <QFile>
    #include <QApplication>
    #include <QLabel>
    #include <QString>
    #include <QTextStream>
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QFile file("/home/pragati/in.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QTextStream in(&file);
    QString line = in.readLine();

    QLabel label(line);
    label.show();

    return app.exec();
    }
    @

    But the same thing i do in the following way :
    @

    #include "widget.h"
    #include "ui_widget.h"
    #include <QFile>
    #include <QApplication>
    #include <QLabel>
    #include <QString>
    #include <QTextStream>

    Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
    {
    ui->setupUi(this);
    QFile file("/home/pragati/in.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);
    QTextStream in(&file);
    QString line = in.readLine();
    ui->label(line);

    }

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

    It gives the error : /home/pragati/FIleRead/widget.cpp:18: error: '((Widget*)this)->Widget::ui->Ui::Widget::<anonymous>.Ui_Widget::label' cannot be used as a function

    I know there must be some other way. Please anyone can help me in this.....

    Thanks a ton in advance

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

      Should I use a signal and slot to make use that function?

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

        You can set the text like

        @ui->label->setText(line);@

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

          Whoww......How could I miss something like that....I am so grateful to you for pointing out the silliest thing I forgot.....Thanks a lot Sam!!!

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

            You are welcome .

            1 Reply Last reply
            0
            • P Offline
              P Offline
              pragati
              wrote on last edited by
              #6

              :)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Sam
                wrote on last edited by
                #7

                Kindly Edit the title and add [Solved] to it.

                Happy Coding :)

                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