You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, June 28th 2012, 9:16pm

UI not seen

Hi
I am beginner in QT and C++ so I'm sorry for my lame question.
I'm using QT designer and I would like to call function from myfile.cpp in
on_pushButton_clicked() function.
The code below generates following error: 2065 'ui' undeclaed idenifier. Replacing by MainWindow.ui.label3(..) generate errors:syntax error:missing ';' before' .' And I don't know how tu call this function properly.

MyFile.h

Source code

1
void MyFunction(void);


myfile.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "MyFile.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 

void MyFunction(void)
{
    ui.label_3->setPixmap(QPixmap(":/icons/MyImage.JPG"));




}



.....

mainwindow.cpp:

Source code

1
2
3
4
5
6
7
#include "MyFile.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
....
void MainWindow::on_pushButton_clicked() {  MyFunction(); }

2

Friday, June 29th 2012, 12:39am

read sig (How do we know what 'ui' is if you dont show its definition :rolleyes: :thumbdown: ).

(p.s. 'ui' is probably a pointer, not an object => use ui-> not ui.)
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

3

Friday, June 29th 2012, 9:30am

Thank You for reply.
Whole my code is below. I use QT designer.

Source code

1
2
3
4
5
6
#ifndef CURRENTSOURCES_H
#define CURRENTSOURCES_H

void CurrentSource1_init(void);

#endif // CURRENTSOURCES_H[code] :pinch: 


[/code]


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT
    
    friend void CurrentSource1_init();

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

    void on_pushButton_clicked();

    void on_pushButton_2_clicked();

    void on_pushButton_3_clicked();

    void on_pushButton_4_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H




CurrentSource1.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
#include "CurrentSources.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QLabel>


void CurrentSource1_Init(void)
{
    MainWindow.ui->label_3->setPixmap(QPixmap(":/ikonki/ikonki/CurrentSource1.JPG"));

}



main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    
    return a.exec();
}



mainwindow.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "CurrentSources.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QLabel>

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


    ui->setupUi(this);
}

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


void MainWindow::on_pushButton_clicked()
{
    CurrentSource1_init();
}

4

Friday, June 29th 2012, 12:53pm

do you still have a problem?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

5

Friday, June 29th 2012, 3:33pm

Yes, I forgot copy errors:
C:\QT_projekty\Wspomaganie2-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Release\..\Wspomaganie2\CurrentSource1.cpp:9: błąd:C2143: syntax error : missing ';' before '.'

C:\QT_projekty\Wspomaganie2-build-desktop-Qt_4_8_1_for_Desktop_-_MSVC2010__Qt_SDK__Release\..\Wspomaganie2\CurrentSource1.cpp:9: błąd:C2143: syntax error : missing ';' before '.'

6

Friday, June 29th 2012, 6:33pm

MainWindow is class. You cannot call normal member methods or access member variables on a class. You can only access them through a class INSTANCE.

In other words

Source code

1
2
3
4
MainWindow.ui; // NO!

MainWindow mw; // make the instance!
mw.ui; // ok - but ONLY if 'ui' is public!
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

7

Friday, June 29th 2012, 6:36pm

why the frick are you doing that 'thing' with currentsource1 anyway? Why isn't it normal member method of MainWindow?

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class MainWindow
{
...

private:
void init();
}

// cpp
void
MainWindow::init()
{
  ui->label_3->setPixmap(QPixmap(":/ikonki/ikonki/CurrentSource1.JPG"));
}
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

8

Tuesday, July 3rd 2012, 12:26pm

Thanks for Your replies and for Your patience:)
Making this function member resolved the problem.:)

Used tags

qtdesigner