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

Ui not in scope



  • 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.



  • What are you trying to achieve?

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



  • 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]



  • 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.



  • 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?



  • [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()
    {
    }
    "@"



  • 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();
    } 
    

    }
    @



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


Log in to reply