[Solved] *ui not recognized



  • Hello,

    I'm trying to change my ui data (ui_mainform.h automatically created from my .ui file) thanks to the pointer *ui, but it is only recognized in my constructor, and not in the functions which are in the same file mainfrom.cpp..
    I know that I'm using the single and multiple inheritance approaches alltogether, but I couldn't have access to my widgets with only the multiple inheritance because they were not static (I can't change them into static in my ui_mainform file because it is each time reinitialized), and with only the single inheritance approach I think that the ui_mainform.h is no more automatically created.. right?
    Could you please write me some advises about that? I don't know how to make work my connect.

    mainform.h:

    @#include "ui_mainform.h"

    namespace Ui {
    class MainForm;
    }

    class MainForm : public QMainWindow,
    private Ui::MainForm // ui_mainform.h automatically created
    {
    Q_OBJECT

    public:
    explicit MainForm(QWidget *parent = 0);
    ~MainForm();
    static void insertTextInErrorBrowser(QTextEdit *line);

        private:
    

    Ui::MainForm *ui;
    };@

    mainform.cpp:

    #include "mainform.h"

    @MainForm::MainForm(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainForm)
    {
    ui->setupUi(this);

    ui->errorBrowser->setText("hello");

    }

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

    void MainForm::insertTextInErrorBrowser(QTextEdit *line)
    {
    connect(line, SIGNAL(textChanged()), ui->errorBrowser, SLOT(insertPlainText(QString)));
    }@

    As I said, what is really strange is that ui is recognized in the MainForm constructor, but not in the insertTextInErrorBrowser function..


  • Lifetime Qt Champion

    Hi,

    You have insertTextInErrorBrowser a static method. You can't access private members in a static method



  • Thank you for your reply! Now it is recognized, but I can not call the function in another .cpp file, because it is not static anymore

    @\errorMsg.cpp(51): error C2352: 'MainForm::insertTextInErrorBrowser' : illegal call of non-static member function@

    I think that I have to change my code to link the function to a specific object



  • It's better now: I have just some errors about MainForm which are not right, and I don't know why, because the structure seems ok..

    The code is:

    main.cpp:

    @int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    MainForm window;
    errorMsg error;
    error.writeErrorMsg(window," coucou",2);@

    errorMsg.h:

    @class errorMsg : public QObject
    {
    public:
    errorMsg();
    ~errorMsg();
    void writeErrorMsg(MainForm window, QString msg, int value);
    };@

    errorMsg.cpp:

    @void errorMsg::writeErrorMsg(MainForm window, QString msg, int value)
    {
    QTextEdit *line = new QTextEdit("Hello");
    QColor colorLine;
    QFont bold;
    bold.setBold(true);

    if (value==1)
    {
    colorLine.setRgb(191,13,9);
    line->setTextColor(colorLine);
    line->setFont(bold);
    }
    else ...

    window.insertTextInErrorBrowser(line);
    }@

    In the three programs, the MainForm declaration makes some trouble with errors like that:

    @errorMsg.h(29): error C2061: syntax error : identifier 'MainForm'
    error C2511: 'void errorMsg::writeErrorMsg(MainForm,QString,int)' : overloaded member function not found in 'errorMsg'@

    It says that the function is overloaded, but the element number is the same everywhere.. Would you know what I have to change to remove the errors?


  • Lifetime Qt Champion

    You can't copy QObject objects nor derived class from QObject.

    And errorMsg doesn't know anything about MainForm since you didn't include mainform.h

    I would recommend taking a look at Qt's demos and examples to get the basics of how Qt works.

    It looks like you are also a beginner in C++, I would recommend getting a good book about it before going further



  • When you say that I can not copy QObject objects, you are talking about MainForm?

    sorry I didn't write the includes but I already included the mainform.h in errorMsg.h

    I will search some books


  • Lifetime Qt Champion

    errorMsg, MainWindow, basically anything that derives from QObject



  • Ok, I just don't see where I copy these objects.. You mean that I can't use it in other programs like a normal object?

    For instance, I can not have a function with MainForm in it like
    @void writeErrorMsg(MainForm window, QString msg, int value);@
    ??

    Sorry to ask that question, it is probably very easy, but I would need to understand the problem to seek the solution



  • As SGaist said, every object deriving from QObject cannot be copied, your function must therefore be:

    void writeErrorMsg(MainForm& window, QString msg, int value);
    

    or

    void writeErrorMsg(MainForm* window, QString msg, int value);
    

    In your function you're (maybe wihout knowing it) making a copy of the window variable, which is then used in the function's body. Since copying a QObject is impossible by design you're not allowed to use this kind of syntax.

    Edit: you can have a look at this to get an overview of how you can pass parameters to a function



  • Thank you for your explanations.

    I changed it everywhere but the same errors remain. They are all focalised on the use of MainForm in the function writeErrorMsg. I guess that I still don't use it well


  • Lifetime Qt Champion

    Can you show the latest version of your code ?



  • Sure. I put everything.

    mainform.h:

    @#ifndef MAINFORM_H
    #define MAINFORM_H

    #include "ui_mainform.h"
    #include <QMainWindow>
    #include <QString>
    #include <iostream>
    #include <QTextEdit>

    namespace Ui {
    class MainForm;
    }

    class MainForm : public QMainWindow
    //private Ui::MainForm
    {
    Q_OBJECT

    public:
    explicit MainForm(QWidget *parent = 0);
    ~MainForm();
    void insertTextInErrorBrowser(QTextEdit *line);

    private:
    Ui::MainForm *ui;
    };

    #endif@

    mainform.cpp:
    @
    #include <QtGui>
    #include "mainform.h"

    MainForm::MainForm(QWidget *parent)
    : QMainWindow(parent),
    ui(new Ui::MainForm)
    {
    ui->setupUi(this);
    ui->errorBrowser->setText("hello");
    }

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

    void MainForm::insertTextInErrorBrowser(QTextEdit *line)
    {
    connect(line, SIGNAL(textChanged(QString)), ui->errorBrowser, SLOT(insertPlainText(QString)));
    }@

    errorMsg.h:

    @#include "mainform.h"

    #ifndef ERRORMSG_H
    #define ERRORMSG_H

    class errorMsg : public QObject
    {
    //Q_OBJECT

    public:
    errorMsg();
    ~errorMsg();
    void writeErrorMsg(MainForm& window, QString msg, int value);
    };

    #endif@

    errorMsg.cpp:

    @#include "errorMsg.h"

    errorMsg::errorMsg()
    {}

    errorMsg::~errorMsg()
    {}

    void errorMsg::writeErrorMsg(MainForm& window, QString msg, int value)
    {
    QTextEdit *line = new QTextEdit("Hello2");
    QColor colorLine;
    QFont bold;
    bold.setBold(true);

    if (value==1)
    {
    colorLine.setRgb(191,13,9);
    line->setTextColor(colorLine);
    line->setFont(bold);
    }
    else ... (idem)

    line->setReadOnly(true);
    window.insertTextInErrorBrowser(line);
    }@

    main.h:

    @#include <QApplication>
    #include "errorMsg.h"

    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);

    MainForm window;
    errorMsg error;
    error.writeErrorMsg(window, "coucou", 2);
    window.show();

    return 0;
    }@


  • Lifetime Qt Champion

    Your logic is pretty strange, you are creating a read-only QLineEdit and connect it to your errorBrowser. Line edit that will not be shown and never be released.

    What is it that you want to achieve ?



  • Yes the ReadOnly for "line" is useless because the browser is ReadOnly already. My logic is just to make all the changes about the line before and insert it simply in the QTextEdit errorBrowser

    What I would like is creating a errorBrowser which can display some defined errors, and also I need to insert a "line" into it.

    The errorBrowser is shown thanks to my ui_mainform.h and main.h files, so I thought that the inserted "line" was shown too, right?


  • Lifetime Qt Champion

    Then you should have an error class that represent only the error and that returns the corresponding string (e.g. a lighter version of the QSqlError class)

    Why do you need that additional line ?



  • I use that line because I have to change the writing style before inserting it in the browser (after it is too late). I don't think that I can change the colour and style of the written text directly in my connect to my browser, or maybe it is possible but I don't know how..


  • Lifetime Qt Champion

    Then you should have a look at QTextDocument to see how you can modify blocks of text to show how you want



  • I found another way: I put my writeErrorMsg function in my mainform files instead of in my errorMsg files, and it works fine now! Thank you for your advises


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.