View Full Version : How do I add a toolbar to a tab in design mode?

6th December 2016, 18:49

This is my first time using QT, and I have been playing around in the QT creator's Qt widget application. I'm trying to see how to accomplish these tasks:

1. I am currently trying to add a toolbar within a tab, instead of the Main Window, directly in the designer. I tried editing the ui file (by opening it in plain text, modifying, and then rebuilding, but ended up with too many errors.) I'm curious to know how I can do this?

2. Also, I have menu items in a menu, and I want to add a new tab with respect to every menu item, as the user clicks on the menu.
Is there an example that could point me to implementing the same?

3. Does Qtablewidget support excel filter based column filtering, and drag and drop grouping by columns?


6th December 2016, 23:45
I am currently trying to add a toolbar within a tab

Are you talking about replacing or adding to the text and icon that appear at the top of the tab? That would be a rather strange design. It probably would not be easy to achieve. QTabWidget / QTabBar have prescribed appearance and behavior in response to keyboard and mouse actions. In order to implement a toolbar, you would have to override this behavior so that clicks on your toolbar items could be detected and handled. You'd probably also have to reimplement the QTabBar::paintEvent() to draw your tool bar in addition to the text, as well as handle the resize and layout issues. None of this will be easy to do or to debug.

If that's not what you mean, then you'll have to explain more and maybe draw a picture.

I want to add a new tab with respect to every menu item, as the user clicks on the menu.

Connect a slot to the QMenu::triggered() signal for each top-level QMenu you add to your QMenuBar. Create your tab in the slot. Of course, you probably want to ensure that the user doesn't create a new tab every time the same menu item is clicked if a tab has already been created for it.

excel filter based column filtering

What's "excel filter based column filtering"? QTableWidget isn't Excel; it is a generic widget for displaying tables. If you mean, can you show / hide columns based on some external criterion, yes, through QTableView::setColumnHidden().

drag and drop grouping by columns

You mean drag and drop to rearrange columns? Yes, through QHeaderView::setSectionsMovable() and QTableView::horizontalHeader().

If you want to implement a lot of custom functionality or filter rows based on more complex logic, then you would be better off going to the Model / View architecture and implementing a custom QAbstractItemModel and using QTableView in conjunction with QAbstractProxyModel / QSortFilterProxyModel.

7th December 2016, 23:49
Thank you for your response.

1. https://www.ischool.utexas.edu/technology/tutorials/office/mail_merge/images/ribbon_PC01_lg.jpg

I am trying to create a 'qtoolbar' within a 'Qtabwidget'. The Qtabwidget is within the main window. Currently, I can add a toolbar only at the qmainwindow level, upon selecting 'add toolbar' in the context menu of QMainWindow. I am not able to add the toolbar to a tabwidget, as it seems to only be a property of the qmainwindow. In that case, I do not know if there is a way to embed another qmainwindow inside the tab itself and have it's toolbar show. I understand that there would be a way to do it in a programmatic way, but I was wondering about how to go about this.

2. Thank you, I was able to implement the tab creation on menu item click.

3. http://www.jkp-ads.com/images/ExcelTable06.gif

I am trying to see if there is an inbuilt way to create the same filter as above. A list of values unique to a column and a checkboxes to filter the items based on this.

4. https://documentation.devexpress.com/WindowsForms/HelpResource.ashx?help=WindowsForms&document=img2727.jpg

By Grouping, I mean something equivalent to the above image.


8th December 2016, 17:37
I am trying to create a 'qtoolbar' within a 'Qtabwidget'. The Qtabwidget is within the main window.

1 - Ah, this is a Microsoft "ribbon bar (https://en.wikipedia.org/wiki/Ribbon_(computing))." It's really a menu where the menu items are represented as groups of icons that appear below it. There is a commercial product called "QtitanRibbon (http://www.devmachines.com/qtitanribbon-gallery.html)" that does this. I don't know if that is an option for your project. There is also a QRibbon (https://sourceforge.net/projects/qribbon/) project on SourceForge which has been inactive since 2014.

If you want to implement something like this yourself, I think I would start by using QDockWidget as a container for a QTabWidget. The widget contained on each tab would contain the buttons and groups for the commands for that tab. This is not an easy project.

3 - Wow, you set high bars for yourself. Basically, you would use QTableWidget::setCellWidget() to set a QComboBox in the cell. I have no idea how you would insert a combo box into the popup as your example shows. I think ultimately you will need to design a custom combobox widget for this and use it as the cell widget.

4 - This isn't available in QTableView / QTableWidget. You might be able to use QTreeView / QTreeWidget for this, but tree views don't draw grid lines. Maybe something like CuteReport (https://cute-report.com) can do this.

If you are developing this GUI as part of a project and not for homework, there are a lot of resources out there, both commercial and open-source. Using / adapting some of these might save you a lot of work. Look at the inqlude (https://inqlude.org/) site or under the "Qt Programs" topic on linux-apps.com (https://www.linux-apps.com/browse/ord/latest/).

13th December 2016, 19:00
Thank you very much for your response, I really appreciate your help! Yes, this was actually for a project at work. ^^;

I have yet another question on QTableView.

We have a requirement where in our datasource for the qtableview comes from the server in the form of a json.
Could you please point me to an existing example which implements the above functionality?
I have already looked at jsonlistmodel(for qml) and save game example, but I'm not able to figure out how to implement the same (and feed data in a json format to the tableview) for a qt widgets application in c++.

Is there a way to implement the jsonlistmodel in c++? (someway I can specify the url directly, obtain the json object and pass it as a parameter to the table?)

Thank you again!

14th December 2016, 11:30
You could parse the JSON data using Qt's JSON classes, e.g. see QJsonDocument, and then either feeding the data into a custom model, derived from QAbstractTableModel, or adding it to a QStandardItemModel.