How to call an object created in another function.
-
Hello
I want to call an object of class QLabel in another function . I know this is basic C++ ,but its confusing in Qt .
in my mainwindow.cpp i have created a function
void createlbl(){ QLabel *lbl1 = new QLabel("Hello"); } void Mainwindow :: resizeEvent(QResizeEvent *evt){ }
i need to call the label object (lbl1) in the resizeEvent function to resize it.
I also need to call the object in other functions in my program.
I tried different ways but i am not getting the right way to call it and use in different functions.
Please tell me what parameters should i pass and how i should call the class or the function in which the class is created to use in different functions.Thank you
-
hi
Object in other functions are not accessible to others unless used as parameter.
The trick is to declare the object in the .h file for the class.
so
QLabel *lbl1; shoud live in mainwindow.h in the class. (public)class MainWindow : public QMainWindow
{
Q_OBJECTpublic:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
QLabel *lbl1; //<<<< < this is shared in all functions
};then it all works out for you.
void createlbl(){
lbl1 = new QLabel("Hello");
}void Mainwindow :: resizeEvent(QResizeEvent *evt){
lbl1 -> xxx
} -
@ronyNS
super :)
just a note:
you can get label to fit window automatically- if you use layouts. -
Yes i am aware of that , but this is the problem https://forum.qt.io/topic/69999/how-to-add-a-label-on-a-layout-that-covers-the-entire-window/2
This is the reason i am using resizeEvent. -
@ronyNS
ok :)
Still not sure why layout wont work but resize event will then.
You just want qLabel to cover all mainwindow ? -
Yes , like i want to cover my login form , but when i drag the label on the ui it resizes according to the layout.
I used size policy , but when i run it , the main window size is fixed and cannot be reduced.
If there is an easy way , please tell me :) -
@ronyNS
ok, sounds like i dont understand the full setup then.
Maybe resize event will just be easier :) -
Yes , Thank you ! :)
-
Hey now i am getting 'Expected specifier before Qlabel' error . Please help
-
mrjj Lifetime Qt Championreplied to ronyNS on 9 Aug 2016, 14:31 last edited by mrjj 8 Sept 2016, 14:32
@ronyNS said:
Expected
show line where it says it :)
did u include <QLabel>
in mainwindow. h?if its for the line
QLabel *lbl1; -
void createlbl(){ lbl1 = new QLabel("Hello"); }
Error is for this line only when i use lbl1 in other functions. Yes i have included Qlabel in .h file
-
mrjj Lifetime Qt Championreplied to ronyNS on 9 Aug 2016, 14:56 last edited by mrjj 8 Sept 2016, 14:57
@ronyNS
well u are not in mainwindow method..void XXXX::createlbl(){
XXXX should be mainwindow
when there is no NAME:: in front, its NOT a member function. just a plain function.
-
mrjj Lifetime Qt Championreplied to ronyNS on 9 Aug 2016, 15:15 last edited by mrjj 8 Sept 2016, 15:18
but it should work. ?
show mainwindow.h
here is sample that does so
https://www.dropbox.com/s/i0qj2e9mxbggymt/mylabel.zip?dl=0 -
my mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include<QLabel> #include <QMainWindow> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); QLabel *lbl; protected: void resizeEvent(QResizeEvent *evt) override; private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
my mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include<QLabel> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); } void MainWindow:: createlbl(){ lbl = new Qlabel(); } void MainWindow :: resizeEvent(QResizeEvent *evt) { MainWindow::setCentralWidget(lbl); lbl->setStyleSheet("Background-color:pink"); QMainWindow::resizeEvent(evt); //call base implementation } MainWindow::~MainWindow() { delete ui; }
-
3/17