View Full Version : Best way to access members in MainWindow from a child class?
dmginc
17th January 2011, 12:06
In my MainWindow class, I'm creating a class and passing QLabels like so:
new_class = new NewClass(QLabel1, QLabel2);
NewClass constructor:
NewClass::NewClass(QLabel *qLabel1, QLabel *qLabel2) : qLabel1(qLabel1), qLabel2(qLabel2)
{
}
NewClass definition
class NewClass : public QObject
{
Q_OBJECT
public:
NewClass(QLabel *qLabel1, QLabel *qLabel2);
private:
QLabel *qLabel1;
QLabel *qLabel2;
}
The above method works fine and I am able to access and directly modify the QLabels in the main window from new_class. However, I am wondering if there is a better way to do this?
I believe this method will get quite tedious when I may need to access and modify many more objects contained in the MainWindow (as I will have to pass every single widget!).
Can I just pass a reference to the entire MainWindow?
Any help or insight would be kindly appreciated :)
stampede
17th January 2011, 12:45
Assuming that you only want to update label's text, maybe better approach could be not passing any single widget pointer at all ?
MainWindow owns the labels, so better design IMHO would be to define a class / struct for data modified in NewClass:
typedef struct{
QString labelText1;
QString labelText2;
QWhatever otherData;
} data_to_pass;
Then define slot in MainWindow:
void MainWindow::updateData(const data_to_pass& newData);
and signal in NewClass:
signals:
void sendNewData(const data_to_pass& newData);
Then you could add new data just by modifying the struct and MainWindow's updateData slot implementation.
You can just modify the struct slightly to get control over more label's parameters ( size, position, text color ect. )
wysota
17th January 2011, 13:12
I'd say that you should never need to pass pointers to widgets to some other widgets. Either use signals and slots directly or delegate the access to the widget to a class that owns the widget. In your case MainWindow should be the only class accessing the labels directly.
dmginc
17th January 2011, 13:58
Assuming that you only want to update label's text, maybe better approach could be not passing any single widget pointer at all ?
MainWindow owns the labels, so better design IMHO would be to define a class / struct for data modified in NewClass:
typedef struct{
QString labelText1;
QString labelText2;
QWhatever otherData;
} data_to_pass;
Then define slot in MainWindow:
void MainWindow::updateData(const data_to_pass& newData);
and signal in NewClass:
signals:
void sendNewData(const data_to_pass& newData);
Then you could add new data just by modifying the struct and MainWindow's updateData slot implementation.
You can just modify the struct slightly to get control over more label's parameters ( size, position, text color ect. )
That's a nice idea.
However, I am actually using setPixmap() to show frames in the QLabel
i.e.
qLabel1->setPixmap(QPixmap::fromImage(frame));
So I would like to work DIRECTLY with qLabel1 within new_class.
@wysota: So passing a reference to QLabel to new_class isn't a good idea?
wysota
17th January 2011, 14:05
@wysota: So passing a reference to QLabel to new_class isn't a good idea?
No, it isn't. If you want to show frames of an animation then either use QMovie or emit frames with a signal and connect them to a slot in QLabel.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.