Unsolved Accessing label from function gives error
-
Hi everyone,
I am new to QT. I created an application by a wizard. Its UI backend was created as below.
MainWindow::MainWindow(QWidget *parent)
:QmainWindow(parent)
,ui(new UI::MainWindow) {
}I placed a label on UI with name lbl. I created a timer and a function called myFunc() to update the time on label (lbl). I want to update the label (lbl) with time on every 1 sec tick of timer. I connected the timer signal with slot (myFunc). In myFunc I want to access the label to update the text with correct time but it gives me error.
I want to know two things, in this auto creation of MainWindow class how can I declare private and public data and member functions and second how can I access this lbl from myFunc().
Help will be appreciated.
-
@Imran-Hassan said in Accessing label from function gives error:
it gives me error
What error?!
"how can I declare private and public data and member functions and second how can I access this lbl from myFunc()" - C++ basics? Is myFunc member of your MainWindow?
You should really post your MainWindow code...
-
Let me paste the full code. In TimerSlot() function it is giving error "Use of undeclared identifier ui"
QTimer *timer; // NEW
void TimerSlot(); // NEW slot
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
//saveSetting();
loadSettings();
ui->setupUi(this);
// this->layout()->setSizeConstraint(QLayout::SetFixedSize);
const QString time = QDateTime::currentDateTime().toString();
ui->currentDateTime->clear();
ui->currentDateTime->setText(time);
timer = new QTimer(this); // create it
connect(timer, &QTimer::timeout, this, TimerSlot); // connect it
timer->start(1000); // 1 sec timer
}void TimerSlot()
{ui->lbl.setText(QDateTime::currentDateTime().toString());
} -
@Imran-Hassan said in Accessing label from function gives error:
void TimerSlot()
Shouldn't it be
void MainWindow::TimerSlot()
?
-
void TimerSlot()
This is simply a global function. You need it to be a member of
MainWindow
, at least if you expect it to accessui
. -
@jsulm and @JonB Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.
Can you guide me in this particular scenario? I mean using this above code. I can if I create new application and declare my own class of MainWindow but I have already done a lot of work to create GUI so I want to fix in this code.
Secondly this QT is now allowing me to reply within 10 minutes without earning a reputation.
-
@Imran-Hassan said in Accessing label from function gives error:
Yes it should be but in this auto-created code, I am not able to know how to declare public and private member functions and data.
The auto-generated code goes into a file named
ui_mainwindow.h
in your debug/release directory. This is not a file you see or edit in Creator, and you should not edit this file. You have your ownmainwindow.cpp
/.h
which#include
s that file. There you declare your ownMainWindow
class which inherits fromUi::MainWindow
. This file is generated initially by Creator, but thereafter is not altered. You can & should make your own changes in your ownmainwindow.cpp
/.h
files. -
@Imran-Hassan I got the answer .... To help other here is the answer
can do that in the mainwindow.h file.
class MainWindow {
//...
private slots:
void TimerSlot();
}; -
@JonB Thanks a lot dear.. For such nice help. Can you upvote this question so that I can gain reputation to ask questions frequently
Anyway thanks again for such nice help