Solved Access To QWidget declared in addTab() function.
-
More information in the function documentation.
-
@mrjj
Hey, what does that It statement do? And what do I have to write instead of xxxxx() or is it what I actually have to write? -
Hi
Hey, what does that It statement do? And what do I have to write instead of xxxxx()?
Hi. this statement takes a base pointer (QWidget *) and cast it to the concrete child type.
In this case a QTextEdit.
This only works it IS a QTextEdit there.
So basically we do likeSomeClass * ptr = ( Otherclass *) ui->tabWidget->widget(0)
but in a saer manner that will fial its its not a QTextWidget.
A pure cast /old type cast will always work but crash the app if
it really not is a TextEditedit->xxxxx(); is just to show to call functions in it. xxxx is just like any function it has.
like
edit->toPlainText():to get all text into QString or what ever you want to do with your textedit
-
@mrjj
Ok thanks, but I have two more question.- Can declared that QTextEdit as a global pointer?
- what does the '0' mean in ui->tabWidget->widget(0)?
That's it, no more question, thank you for your patience and sorry because I'm new!
-
-
no there's no need for a global variable, you can use a member variable but do not start using globals for that.
-
it's explained in the method documentation. It's the index of the widget you want to get.
-
-
@SGaist
Oh ok, but I need it as a global, can I declare it like that?
Thank you for explaining me the widget(0) thing! -
@HenkCoder
Hi
Why would you need it as a global ?If you plan something like NotePad++ with tabs then each tab will be its own QTextEdit
and you need to take the right one, depending on which tab user clicks.So you should hook up a slot to the signal
https://doc.qt.io/qt-5/qtabwidget.html#currentChanged
and there you got the index (the parameter) to use to ask for the widget, then
convert it and you can alter text or what you want. -
@mrjj
Hey, I need it as a global because I want to put other buttons like bold, italics, underline etc, but I cannot access the QTextEdit if it's not a global -
Hi
You still dont need a global variable for that :)Any button that must access the TextEdit can just get it.
like you cna make a helper function to make this easy
TextEdit * MainWindow::GetEditor() { return q_objectcast<TextEdit *> ( ui->tabWidget->currentWidget() ); } then in BOLd button clicked etc , you can do void BoldClicked() { TextEdit * edit = GetEditor(); to have access to the current tab as a TextEdit if (edit) { make text bold ... } }
-
@mrjj
Oooh, I understand.
Thank you all for the help!