Solved Keep focus on QTextEdit while clicking toggle button on multiple QTextEdit windows
-
Ok I want to setup multiple QTextedt windows, and buttons like bold,italic etc.
Now this is the problem I have cheated, but it works, but this won't work on multiple QTextedits.
You see the problem is that when you click on the toggle button you use lose focus.
Now I have got around this on one window by re-focusing on the QTextedit window.eg
void MainWindow::on_pushButton_3_toggled(bool checked) { if(checked==true){ ui->textEdit->setFontWeight(QFont::DemiBold); }else{ ui->textEdit->setFontWeight(QFont::Normal); } ui->textEdit->setFocus(); }
But I want to use the same button on multiple QTextviews.
The above code will only work on one single QTextedit called textEdit.I wonder if there is a way to get the last object that lost focus when you click on a toggle button.
This way I can set the font to bold on multiple QTextedit windows, rather than hard coding in a single Qtextview like I have here.I don't really want to use multiple buttons, but have one button strip that can be use for more than one text view.
-
Maybe by setting the focus to NoFocus in Qt Designer or by code :
toggleButton->setFocus(Qt::NoFocus);
-
@mpergand
Great that has answered half my question. This really works coolNow all I have to do is to get the window that has focus. I think I found the command to do it, but don't think I am doing it right.
QWidget * thing = qApp->focusWidget();
Instead of
ui->textEdit->setFontWeight(QFont::DemiBold);
I thought I could do this, but it doesn't work
thing->setFontWeight(QFont::DemiBold);
So I thought I could do something like this
QWidget * thing = qApp->focusWidget(); if(checked==true){ thing->setFontWeight(QFont::DemiBold); }else{ thing->setFontWeight(QFont::Normal); }
But thing should be the textEdit window, but why I can't I do thing->setFontWeight.
-
you need qobject_cast
Have a look at the doc. -
This post is deleted! -
@mpergand
Hey I got it to work thanks.
It took some time because I always find that the docs tell you everything, but what you want to know LOL.
Anyway here is my code nowvoid MainWindow::on_pushButton_3_toggled(bool checked) { QWidget * thing = qApp->focusWidget(); QTextEdit *test = qobject_cast<QTextEdit *>(thing); if(checked==true){ test->setFontWeight(QFont::DemiBold); }else{ test->setFontWeight(QFont::Normal); } }
I might set up QWidget and the QTextEdit variables in the header file later so they not constantly being recreated, but basically it works, and I might pick better variable names LOL.
Thanks I can now make text bold in 2 QTextEdit windows.
-
It's advisable to check the value of a dynamic cast/qobject cast because it can be null if the object cannot be converted to the specified type.
-
@mpergand
I think this will do the trick, thanks;QWidget * thing = qApp->focusWidget(); QTextEdit *test = qobject_cast<QTextEdit *>(thing); if(test!=NULL){ if(checked==true){ test->setFontWeight(QFont::DemiBold); }else{ test->setFontWeight(QFont::Normal); } }