c++ - Qt checkable actions -
i'm making rich text editor , i'm having trouble checkable actions. right have:
void wordwritemainwindow::on_actionitalic_toggled(bool arg1) { if(arg1==true) { ui->textedit->setfontitalic(true); } else { ui->textedit->setfontitalic(false); } }
for part works. but, want make action checked when you're type italics. specific things make action checked while you're not writing in italics. example: if typing , click area italic start typing in italics, action unchecked. or if highlight , click italic, , click away somewhere not italic checked , won't typing in italics.
do need use signals , slots? or maybe kind of if-else ladder? thank concern. forward fixing annoying issue. , forward many future road bumps because there few when comes saving file, etc.
first of all, code is not terribly readable. why not replace "bool arg1" more descriptive "bool enabled" or "bool italic"? after that, can make function trivial one-liner:
void wordwritemainwindow::on_actionitalic_toggled(bool italic) { ui->textedit->setfontitalic(italic); }
going actual question, yes, there has "something" ties action state qtextedit
thinks active -- far, have specified coupling in opposite direction. qtextedit provides currentcharformatchanged(const qtextcharformat &) signal can connect to. connect slot of yours , update action checked state form it:
void wordwritemainwindow::updateactionstatefromformat(const qtextcharformat &f) { ui->youritalicaction->setchecked(f.fontitalic()); }
you have connect signal slot, of course. easiest way add constructor:
wordwritemainwindow::wordwritemainwindow() { // existing code goes here connect(ui->textedit, signal(currenttextformatchanged(qtextcharformat)), this, slot(updateactionstatefromformat(qtextcharformat))); }
Comments
Post a Comment