I see that QMdiArea has a tabbed view mode. I want to be able to split the main window with two QMdiArea widgets and to be able to drag and drop tabs between each of them. I have already done it with a simple QTabWidget where I can set custom tab bar. At the same time I want to switch QMdiArea view mode thus using QTabWidget is not an option for me. But I don't see any methods to set custom tab bar within QMdiArea. I still have hope that it could be done. Can anyone suggest something?
Tested solution for Qt 4.8 (edit)
After some time of research I can suggest the following solution. You have to make a new class inheriting QMdiArea. Set its view mode to TabbedView to make the standart QTabBar to be constructed within QMdiArea. Then get all children and find QTabBar widget with QString(QObject::metaObject()->className()) == "QTabBar". Hide it. You will get a blank area above the document in TabbedView mode. Construct you custom tab bar and reparent it to your custom mdi area. Connect signals and slots that are fired and used when the sub windows and tabs are activated. You can have your custom tab bar as a class member of your custom mdi area.
If you have found this post useful please vote on it. Thanks.
Some code for example.
Looking for a standart QTabBar within a custom mdi area in its constructor:
m_pMdiAreaTabBar = NULL;
m_pMdiArea->setViewMode(QMdiArea::TabbedView);
QObjectList listChildren = m_pMdiArea->children();
for (QObjectList::Iterator i = listChildren.begin(); i != listChildren.end(); ++i)
{
if (QString((*i)->metaObject()->className()) == "QTabBar")
{
m_pMdiAreaTabBar = dynamic_cast<QTabBar*>(*i);
break;
}
}
Reparent:
m_pTabBar->setParent(m_pMdiArea);
Hiding:
if (m_pMdiAreaTabBar != 0) m_pMdiAreaTabBar->hide();
Signals & Slots used: QMdiArea::subWindowActivated(QMdiSubWindow*), QTabBar::currentChanged(int)
Related
I have a design like below:
So basically, I want to embed three dialogs in the application main dialog and switch between them, for each button click i.e., button 1 will show dialog one , button 2 will hide dialog 1 and show dialog 2 .. and so on.
Each dialog will be having a different design and functions.
I tried using CPropertySheet class to Add pages but its GUI is different. It has either option for navigating the dialogs using next / back button , or from a tab control.
None of which is as per my requirement.
So I want to know is it possible to have a design like this in MFC ? If yes how? Which Class/ control should I use.
Any help will be appreciated.
What you can do is use a normal CDialog class, add your buttons to it and also create a frame/rect as a placeholder for where your embedded dialogs are to appear. The following piece of code will create and position your embedded dialog.
CRect rect;
CWnd *pHost = GetDlgItem(ID_OF_YOUR_FRAME_RECT);
pHost->GetWindowRect(&rect);
ScreenToClient(&rect);
pDialog->Create(ID_OF_YOUR_DIALOG, this);
pDialog->MoveWindow(&rect);
pDialog->ShowWindow(SW_SHOW);
On button clicks, you hide the previously shown dialog (SW_HIDE) and show your selected dialog(SW_SHOW) with ShowWindow(...).
If you create your embedded dialogs with IDD_FORMVIEW style in the add resource editor it'll have the proper styles for embedding.
Another option is probably to use an embedded PropertySheet and hide the tab row and programatically change the tabs on the button clicks. I just find it to be too much fuzz with borders, positioning, validation and such for my liking.
If you have the MFC Feature Pack, that first came with VS2008 SP1 and is in all later versions, you might like to consider CMFCPropertySheet. There are a number of examples on the linked page, that are very similar to your design.
For example, this:
What worked for me just using dialog based application is SetParent() method. Dont know why nobody mentioned it. It seems to work fine.
I am doing like below:
VERIFY(pDlg1.Create(PanelDlg::IDD, this));
VERIFY(pDlg2.Create(PanelDlg2::IDD, this));
VERIFY(pDlg3.Create(PanelDlg2::IDD, this));
::SetParent(pDlg1.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg2.GetSafeHwnd(), this->m_hWnd);
::SetParent(pDlg3.GetSafeHwnd(), this->m_hWnd);
Now I can show or hide a child dialog at will (button clicks) as below:
pDlg1.ShowWindow(SW_SHOW);
pDlg2.ShowWindow(SW_HIDE);
pDlg3.ShowWindow(SW_HIDE);
I'm using Qt 4.7 on Windows 7 Ultimate 32 bit.
The QMainWindow of my program has a QDockWidget. I've noticed that if I minimize the main window by the minimize button on the title bar, after restoring it the dock widget is closed. I didn't write any support for a feature like this!
How does this happen and how to prevent this?
Thanks.
I encountered this error when writing my own application. I have QDockWidget with options for my application. Using Qt Creator I created menu with QAction actionMenu which was checkable. Then I connected QDockWidget and QAction like this:
QObject::connect(ui->dockWidget, SIGNAL(visibilityChanged(bool)),
ui->actionMenu, SLOT(setChecked(bool)));
QObject::connect(ui->actionMenu, SIGNAL(toggled(bool)),
ui->dockWidget, SLOT(setVisible(bool)));
The order of connection is not important. And then when I minimized application with QDockWidget being visible, after I restored it QDockWidget was closed and actionMenu was unchecked.
In fact there are two solutions. First which works for me is to use SIGNAL(triggered(bool)) instead of SIGNAL(toggled(bool)):
QObject::connect(ui->dockWidget, SIGNAL(visibilityChanged(bool)),
ui->actionMenu, SLOT(setChecked(bool)));
QObject::connect(ui->actionMenu, SIGNAL(triggered(bool)),
ui->dockWidget, SLOT(setVisible(bool)));
The second solution uses action which you can obtain from QDockWidget:
// Retrieve action from QDockWidget.
QAction *action = ui->dockWidget->toggleViewAction();
// Adjust any parameter you want.
action->setText(QString("&Menu"));
action->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_M));
action->setStatusTip(QString("Press to show/hide menu widget."));
action->setChecked(true);
// Install action in the menu.
ui->menuOptions->addAction(action);
I know for sure that SIGNAL(toggled(bool)) was causing in my application closure of QDockWidget.
I faced the same problem... I managed to get rid of it by using a method called StoreWindowsLayout and RestoreWindowsLayout.
StoreWindowsLayout will save the content of the ByteArray returned by the Method QMainwindow::saveState().
RestoreWindowsLayout will restore that bytearray, and therefore your windows layout, the qdockwidget visibility state and so on...
I call StoreWindowsLayout on ApplicationMainFrm::changeEvent, on ApplicationMainFrm::closeEvent (it's likely this one you'll need) and in ApplicationMainFrm::hide().
Then I use restoreWindowsLayout in ApplicationMainFrm::showEvent.
Exemple of use of restoreWindowsLayout in my MainForm :
void ApplicationMainFrm::showEvent(QShowEvent* pEvent)
{
QMainWindow::showEvent(pEvent);
restoreWindowsLayout();
}
Hope it helps !
I am trying to create a wizard like structure using dialog boxes...So I replaced the code in CDialog1App as below
CDialog1Dlg* dlg = new CDialog1Dlg;
m_pMainWnd = dlg;
dlg->Create(IDD_DIALOG1);
dlg->ShowWindow(SW_SHOW);
the above worked fine...its displying the dialog box.but I have added another dialog box...
So in the first dialog box if the user clicks Next it has to hide the first dialog box and display the second dialog..
//CDialog1 class
void CDialog1Dlg::OnBnClickedNext()
{
// TODO: Add your control notification handler code here
CDialog2* dialog2 = new CDialog2();
dialog2->Create(IDD_DIALOG2);
dialog2->ShowWindow(SW_SHOW);
this->ShowWindow(SW_HIDE);
}
in the above code am creating an object for the Dialog2 class and trying to show that....
Now the problem is,when I click next its hiding both the windows..What can I do..I tried several types but its still its not workin..Please dont suggest me to do with PropertySheet..It will work with that, i know ...but I want this using Dialog Box for some reason
You're creating the dialog2 with the default parent window (NULL):
dialog2->Create(IDD_DIALOG2);
But the default parent seems to be dialog1 in your case. And since you hide dialog1 which is the parent of dialog2, dialog2 is also hidden.
Find the window (CWnd) of either your main app dialog (if you have one visible apart from your wizard), or use the desktop window as the parent.
For example:
dialog2->Create(IDD_DIALOG2, GetDesktopWindow());
In Qt, if I right-click on a toolbar the menu will be shown that allows me to hide the toolbar. I need to disable this functionality because I don't want the toolbar to possible to hide. Is there a way to do this?
I was able to set the ContextMenuPolicy directly on the toolbar (not the main window), as long as I used either Qt::PreventContextMenu or Qt::ActionsContextMenu. Prevent eliminated the context menu and made right-click have no effect on the toolbar, while Actions made a nice context menu composed of the actions already in my toolbar. Qt::NoContextMenu didn't seem to have any effect.
toolbar->setContextMenuPolicy(Qt::PreventContextMenu);
Use setContextMenuPolicy (Qt::NoContextMenu) for the main window of the toolbar.
There are several ways to achieve this without having to alter the contextMenu functionality. See the following 3 PySide examples:
1. Disable the toggleViewAction of the QToolBar:
UnhidableToolBar = QToolBar()
UnhidableToolBar.toggleViewAction().setEnabled(False)
2. Connect to the visibilityChanged signal:
toolbar.visibilityChanged.connect(lambda: toolbar.setVisible(True))
3. Subclass QToolBar and use the hideEvent:
class UnhideableQToolBar(QToolBar):
def hideEvent(self, event):
self.setVisibile(True)
Recommendation:
While 2 & 3 are pretty dirty, solution 1 shows the toolbar in the context menu like a QDockWidget that has the feature DockWidgetClosable set. So either use solution 1 or if you want to remove the action have a look at Steven's answer.
Override QMainWindow::createPopupMenu() e.g.
QMenu* MyApp::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
filteredMenu->removeAction( mUnhidableToolBar->toggleViewAction() );
return filteredMenu;
}
Note that the other answers that suggest disabling the context menu will only work if you want to disable hiding/showing of all toolbars and all dock widgets.
Inherit QToolbar and reimplement contextMenuEvent().
The simplest thing to do is:
self.toolbar.toggleViewAction().setVisible(False)
Unlike self.toolbar.toggleViewAction().setEnabled(False), which still shows the disabled popup if you're right-clicking the toolbar for any reason.
I'm using a QTabWidget to render multiple documents in a window, and I want to draw a close button on each tab. I'm using Vista and Qt4, so the tab widget is a native windows control; this may affect the feasibility.
Does anyone know if it is possible to do this using the QTabWidget control, or do I have to create a custom widget? If creating a new widget is the only option, any pointers would be much appreciated; I'm relatively new to Qt.
Since Qt 4.5. If you just call setTabsClosable(true) on QTabWidget, you will have the close buttons but they won't be bound to an action.
You have to connect the tabCloseRequested(int) signal to one of your own slots if you want the buttons to do something.
MainWindow::MainWindow()
m_tabs = new QTabWidget();
m_tabs->setTabsClosable(true);
connect(m_tabs, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));
void MainWindow::closeTab(const int& index)
{
if (index == -1) {
return;
}
QWidget* tabItem = m_tabs->widget(index);
// Removes the tab at position index from this stack of widgets.
// The page widget itself is not deleted.
m_tabs->removeTab(index);
delete(tabItem);
tabItem = nullptr;
}
In 4.5 there is function
void setTabsClosable ( bool closeable )
Currently there is no way to do this with the stock QTabWidget, however the upcoming Qt 4.5 (planned to be released in March 2009) will have the ability to add close buttons to tabs either manually or by setting a QTabBar.TabsClosable property.
Until then, the only way to get close buttons is to subclass QTabWidget or QTabBar and add it manually (possible, but not trivial).