PDA

View Full Version : acsessing QTextEdit from QTabWidget



ishkabible
14th September 2010, 01:34
so i'm making a small editor for FASM with Qt, i am building over the Syntax Highlighter Example that the Qt SDK comes with. i have syntax highlighting down and file I/O is going really well but i want to have tabs to mange multiple files at the same time. at first i thought "ok that's easy i just make QTabWidget my central widget and add QTextEdit child's to it." the issue with this is that i cant find a way to access the actual QTextEdit becuase i can only access the QWidget of the tab. im not sure how QTextEdit works witch makes finding a solution some what difficult. if there is a way to alter the actual Widget indexed by each tab as a QTextEdit and maintain all needed information to display them correctly.

here is some code showing my problem, how do i alter the properties of the current widget as a QTextEdit.



void MainWindow::openFile(const QString &path)
{
QString fileName = path;
QTextEdit Tabed_Editor(Tabs->currentWidget());
if (fileName.isNull())
fileName = QFileDialog::getOpenFileName(this,
tr("Open File"), "", "ASM Files (*.asm *.inc);; All(*.*)");

if (!fileName.isEmpty()) {
QFile file(fileName);
if (file.open(QFile::ReadOnly | QFile::Text))
Tabed_Editor.setPlainText(file.readAll()); ///here this will not actully edit the current widit as it is a copy but how else would i edit it?
}
}

tbscope
14th September 2010, 05:49
QTextEdit *editor = qobject_cast<QTextEdit *>(Tabs->currentWidget());

...

editor->setPlainText(...);

ishkabible
14th September 2010, 22:12
thanks, i thought this would use a template but i had not seen any documentation that would allow for this kind of conversion (i was only looking within the classes them self's), thanks again :)

tbscope
15th September 2010, 04:33
Just a note:
Always check if the object exists after casting and before using it. Otherwise you'll get a crash.