Create a Toolbar on a Subclassed Textedit?
I was wondering, I managed to successfully subclass QTextEdit into my own custom text edit, but I would like to create a toolbar at the top of the editable area with bold, italic options etc. I halfway did this by passing the Edit control as the parent when I created the toolbar, but it displays transparently, and the text still starts underneath the toolbar control. Is there a way to solve this problem, so that the edit area begins below the toolbar, or is there a better way to attach a toolbar to a textedit control? Thanks!
Re: Create a Toolbar on a Subclassed Textedit?
Put the text edit into a QMainWindow. Then you can place a QToolBar into the QMainWindow. You can put a QMainWindow as a child widget into a layout like any other widget.
Re: Create a Toolbar on a Subclassed Textedit?
Thank you for the reply... I think the problem that I'm having is that I want to show a bunch of different editors on different tabs (some with editors and some with other widgets), but I don't necessarily want the toolbar to be shown at the top of the window; just when a tab with an editor is selected. If it is not possible to attach the toolbar directly to an edit, I suppose I could create/destroy it on the main window instead in my textedit subclass?
I'm trying to learn more about custom controls so I was curious to see if attaching a toolbar to a textedit directly was possible, but I don't have to go that route.
Re: Create a Toolbar on a Subclassed Textedit?
Ok, I solved this problem for now by creating the toolbar on the edit's parent in my custom edit's subclass constructor and resizing the edit to start below it. It should have the same effect....
Re: Create a Toolbar on a Subclassed Textedit?
Here's an example illustrating what I meant:
Code:
#include <QtGui>
int main(int argc, char* argv[])
{
QMenu* fileMenu
= window.
menuBar()->addMenu
("File");
fileMenu->addAction("Quit", &window, SLOT(close()));
for (int i = 0; i < 3; ++i)
{
QMenu* menu
= textEdit
->createStandardContextMenu
();
toolBar->addActions(menu->actions());
wrapper->addToolBar(Qt::TopToolBarArea, toolBar);
wrapper->setCentralWidget(textEdit);
tabWidget
->addTab
(wrapper,
QString::number(i
));
}
window.setCentralWidget(tabWidget);
window.show();
return app.exec();
}
Notice the usage of a wrapper QMainWindow inside QTabWidget.
Re: Create a Toolbar on a Subclassed Textedit?
OK, thanks, I get it now :D