c++ - Getting the text from a QTextEdit inside a QWidget -
i have qtabwidget
'tw' add together tabs this:
qwidget *newtab = new qwidget(tw); tw->addtab(newtab, "tab name"); qtextedit *te = new qtextedit(); te->settext("hello world"); qvboxlayout *vbox = new qvboxlayout(); vbox->addwidget(te); newtab->setlayout(vbox);
how can text within qtextedit
tab in foreground (like when click button i'd re-create text visible tab clipboard or smtg that). don't know how handle of qtextedit
.
you need maintain track of text edits manually. either through storing pointer them in parent widget, or create utilize of lookup table, e.g. qhash:
assuming have class myclass
contains code posted in question:
add fellow member variable this:
class qtextedit; // so-called "forward declaration" saves // #include. google if want know more ;-) class myclass { // ... private: qhash< int, qtextedit* > _texteditpertabpage; };
this variable makes possible store (and find) text edit index (0, 1, 2, ...) of tab pages.
you add together function this:
void myclass::addtab( qwidget* tabwidget, const qstring& tabname, const qstring& text ) { // create text edit qtextedit* textedit = new qtextedit(); textedit->settext( text ); // create layout contains text edit qvboxlayout* layout = new qvboxlayout(); layout->addwidget( textedit ); // create parent widget layout qwidget* newtab = new qwidget( tabwidget ); newtab->setlayout( layout ); // add together widget new tab int tabindex = tabwidget->addtab( newtab, tabname ); // remember text edit widget _texteditpertabpage.insert( tabindex, textedit ); }
and retrieve pointer on qtextedit
this:
qtextedit* textedit = _texteditpertabpage.value( tabwidget->currentindex() );
this code has couple of limitations, e.g. have create sure utilize own myclass::addtab
function , don't access qtabwidget::addtab
outside of function. if phone call qtabwidget::removetab
, qhash
may no longer point proper qtextedits.
c++ qt qt4
No comments:
Post a Comment