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. Ui not in scope
Forum Updated to NodeBB v4.3 + New Features

Ui not in scope

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 2.1k 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.
  • D Offline
    D Offline
    davidrhcp
    wrote on 6 May 2014, 17:44 last edited by
    #1

    This is my code for main.cpp
    @
    #include "mainwindow.h"
    #include <QApplication>
    #include <cstdlib>
    #include <QNetworkConfiguration>
    #include <QDebug>
    #include <QDir>
    #include <QFileInfo>
    #include <QNetworkAccessManager>
    #include <QNetworkConfiguration>
    #include <mainwindow.h>
    #include <QFile>
    #include <QString>
    #include <QIODevice>
    #include <QtGui>

    void Write(QString MBW){

    QFile mFile(MBW);

    if (mFile.open(QIODevice::WriteOnly))
    {
    QTextStream out(&mFile);
    out << ui->lineEdit->text();
    }

    }
    int main(int argc, char *argv[])
    {

    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    // creating directory upon run for user input
    QDir mDir;
    mDir.mkpath("C:/Monice/BW");
    
    
    QString mFile = ("C:/Monice/BW/MBW.txt");
    
    return a.exec(&#41;;
    

    }
    @
    The ui apparently is not in the declared in the scope
    not sure what to do

    [andreyc EDIT]: Added '@' around the code. Please use '@' around your source code, log output, etc.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andreyc
      wrote on 6 May 2014, 18:34 last edited by
      #2

      What are you trying to achieve?

      ui is a member of MainWindow class and is not accessible from standalone function void Write(QString MBW)

      1 Reply Last reply
      0
      • D Offline
        D Offline
        davidrhcp
        wrote on 6 May 2014, 18:58 last edited by
        #3

        im trying to get a push button to save the text entered by a user into a line edit box to save the text to a file in a directory. this is the main.cpp

        @#include "mainwindow.h"
        #include <QApplication>
        #include <cstdlib>
        #include <QNetworkConfiguration>
        #include <QDebug>
        #include <QDir>
        #include <QFileInfo>
        #include <QNetworkAccessManager>
        #include <QNetworkConfiguration>
        #include <mainwindow.h>
        #include <QFile>
        #include <QString>
        #include <QIODevice>
        #include <QtGui>

        void Write(QString MBW){

        QFile mFile(MBW);

        if (mFile.open(QIODevice::WriteOnly))

        {
        QTextStream out(&mFile);
        out << ui->lineEdit->text();
        }

        }
        int main(int argc, char *argv[])
        {

        QApplication a(argc, argv);
        MainWindow w;
        w.show();
        
        // creating directory upon run for user input
        QDir mDir;
        mDir.mkpath("C:/Monice/BW");
        
        
        QString mFile = ("C:/Monice/BW/MBW.txt");
        
        return a.exec&#40;&#41;;
        

        }
        @

        This is the main window.cpp

        @#include "mainwindow.h"
        #include "ui_mainwindow.h"
        #include "QObject"
        #include "QtCore"
        #include "QNetworkConfigurationManager"
        #include "QNetworkInterface"
        #include "QHostAddress"
        #include "QString"
        #include "QFile"
        #include "QLineEdit"
        #include "QDir"
        #include "QApplication"
        #include "QMainWindow"

        MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)

        {
        ui->setupUi(this);

        //display network interfaces
        QList<QNetworkInterface> list = QNetworkInterface::allInterfaces();
        
        // each interface is allocated one after the other in a list
        

        foreach (QNetworkInterface mon, list)

        //adding the list to be shown in the interface combobox
        {
        ui->comboBox_Interface->addItem(mon.name());
        }
        
        // a for loop that counts from 50-90 in multiples of 5
        for (int i = 50; i <= 90; i += 5)
        //selecting the combox alert to diplay the figures 50-90 with a % symbol
        {
            ui->comboBox_Alert->addItem(QString::number(i) + "%");
        }
        

        }

        MainWindow::~MainWindow()
        {
        delete ui;
        }

        QFile::MBW()
        {
        mfile;
        }

        void MainWindow::on_save_clicked()
        {

        }
        @

        and this is the header

        @#ifndef MAINWINDOW_H
        #define MAINWINDOW_H

        #include <QMainWindow>
        #include <QString>
        #include <QObject>
        #include <QFile>
        namespace Ui {
        class MainWindow;
        }

        class MainWindow : public QMainWindow
        {
        Q_OBJECT

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

        private slots:

        void on_save_clicked();
        

        private:
        Ui::MainWindow *ui;
        };

        #endif // MAINWINDOW_H
        @

        [edit: added missing coding tags @ SGaist]

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andreyc
          wrote on 6 May 2014, 19:14 last edited by
          #4

          Please add '@' around your code to make it better readable.

          You use private variable of a class MainWindow in standalone function. It is not going to work in C++.

          Make Write() a member of MainWindow
          or
          provide a getter function in MainWindow for ui and use it in Write.

          1 Reply Last reply
          0
          • D Offline
            D Offline
            davidrhcp
            wrote on 6 May 2014, 19:36 last edited by
            #5

            thanks
            how would i make the write() a function. a lot of QT documents is about the Q strings and so fourth. i dont even understand members, functions and classes, could you suggest a link that may help? also sorry about the code thing, how do i add @ around the code?

            1 Reply Last reply
            0
            • A Offline
              A Offline
              andreyc
              wrote on 6 May 2014, 19:52 last edited by
              #6

              [quote author="davidrhcp" date="1399404981"]i don't even understand members, functions and classes, could you suggest a link that may help? [/quote]
              I would suggest to read some books about C++ to get more info about classes, etc
              "Here is good list of the books":http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list
              "Online C++ tutorial":http://www.learncpp.com/
              "another C++ tutorial":http://www.cplusplus.com/doc/tutorial/

              [quote author="davidrhcp" date="1399404981"]also sorry about the code thing, how do i add @ around the code?[/quote]
              To make your code looks pretty just put character "@" before the code and after.
              Like below but without quotation marks
              "@"
              int main()
              {
              }
              "@"

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on 6 May 2014, 20:00 last edited by
                #7

                In your example try to delete function Write() from main.cpp
                and put its content into void MainWindow::on_save_clicked()

                @
                void MainWindow::on_save_clicked()
                {
                // creating directory upon run for user input
                QDir mDir;
                mDir.mkpath("C:/Monice/BW");

                QString MBW = ("C:/Monice/BW/MBW.txt");
                
                QFile mFile&#40;MBW&#41;;
                
                if (mFile.open(QIODevice::WriteOnly))
                {
                    QTextStream out(&mFile);
                    out << ui->lineEdit->text();
                } 
                

                }
                @

                1 Reply Last reply
                0
                • D Offline
                  D Offline
                  davidrhcp
                  wrote on 6 May 2014, 20:26 last edited by
                  #8

                  thank u, you have been an awesome help! some good sources there as well really appreciated

                  1 Reply Last reply
                  0

                  1/8

                  6 May 2014, 17:44

                  • Login

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