PDA

View Full Version : Create a Toolbar on a Subclassed Textedit?



c_07
12th October 2007, 00:48
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!

jpn
12th October 2007, 04:35
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.

c_07
12th October 2007, 11:39
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.

c_07
12th October 2007, 12:34
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....

jpn
12th October 2007, 15:52
Here's an example illustrating what I meant:


#include <QtGui>

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
QMainWindow window;
QMenu* fileMenu = window.menuBar()->addMenu("File");
fileMenu->addAction("Quit", &window, SLOT(close()));

QTabWidget* tabWidget = new QTabWidget(&window);
for (int i = 0; i < 3; ++i)
{
QMainWindow* wrapper = new QMainWindow(tabWidget);
QTextEdit* textEdit = new QTextEdit(wrapper);
QToolBar* toolBar = new QToolBar(wrapper);
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.

c_07
12th October 2007, 18:17
OK, thanks, I get it now :D