Results 1 to 6 of 6

Thread: Create a Toolbar on a Subclassed Textedit?

  1. #1
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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!

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  3. #3
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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.

  4. #4
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default 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....

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Create a Toolbar on a Subclassed Textedit?

    Here's an example illustrating what I meant:
    Qt Code:
    1. #include <QtGui>
    2.  
    3. int main(int argc, char* argv[])
    4. {
    5. QApplication app(argc, argv);
    6. QMainWindow window;
    7. QMenu* fileMenu = window.menuBar()->addMenu("File");
    8. fileMenu->addAction("Quit", &window, SLOT(close()));
    9.  
    10. QTabWidget* tabWidget = new QTabWidget(&window);
    11. for (int i = 0; i < 3; ++i)
    12. {
    13. QMainWindow* wrapper = new QMainWindow(tabWidget);
    14. QTextEdit* textEdit = new QTextEdit(wrapper);
    15. QToolBar* toolBar = new QToolBar(wrapper);
    16. QMenu* menu = textEdit->createStandardContextMenu();
    17. toolBar->addActions(menu->actions());
    18. wrapper->addToolBar(Qt::TopToolBarArea, toolBar);
    19. wrapper->setCentralWidget(textEdit);
    20. tabWidget->addTab(wrapper, QString::number(i));
    21. }
    22.  
    23. window.setCentralWidget(tabWidget);
    24. window.show();
    25. return app.exec();
    26. }
    To copy to clipboard, switch view to plain text mode 
    Notice the usage of a wrapper QMainWindow inside QTabWidget.
    J-P Nurmi

  6. The following user says thank you to jpn for this useful post:

    c_07 (12th October 2007)

  7. #6
    Join Date
    Oct 2007
    Posts
    17
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Create a Toolbar on a Subclassed Textedit?

    OK, thanks, I get it now

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.