QStackedWidget navigation from page to page - qt

I think i'm having a fairly basic Qt problem, but i can't figure it out:
I have a QMainWindow which holds a QStackedWidget. All page widgets in there are seperate classes derived from QWidget.
So:
QMainWindow implements QStacked Window in one class.
All other pages inside the stacked widget are added classes and all have there own .ui filled with buttons and lists trough the Designer.
For navigating to different pages, inside the Mainwindow i have access to: ui.stackedWidget->setCurrentIndex(2);
It seems i don't have access to ui.stackedWidget on another page inside the stacked widget? I have no access to the ui.stackedWidget because Ui is a private member in the mainwindow class. (auto generated by Qt - using VS addon for adding QT4 classes)
I would like to know, how can i jump to another page in the stacked widget, when clicking on a button that belongs to another page inside this widget?
Note:
All pages are added to the StackedWidget in mainWIndow's constructor:
ui.stackedWidget->addWidget(page1Widget);
ui.stackedWidget->addWidget(page2Widget);
// etc..
Example of a button click signal-slot inside page1Widget:
connect(ui.btnViewData, SIGNAL(clicked()), this, SLOT(viewData()));
::viewData()
{
// navigate to another page here.
// note: ui.stackedWidget->setCurrentIndex(3); is not accessible here!
}

I believe that putting your connect() and viewData() functions within your QMainWindow object will solve your problem, since the main window can have access to both the signals emited by the child widgets and the QStackedWidget items.
You might need to write a Ui getter for each of your page, and then do something like
connect(page1Widget->getUi().btnViewData, SIGNAL(clicked()), this, SLOT(viewData)));
hope it helps,
cheers

Related

Can a QToolbar be added to a QDockWidget?

I have setup my app to have various dock windows within the main window. I am also able to add a toolbar to the main window. However, I would ideally like to add the QToolBar inside one of the QDockWindow instances (or the QWidget that it houses) as the toolbar will be specific to that window.
Is this possible? I'm using a recent version of Qt, 5.10.
I think it is possible.
1.QDockWidget can set a QMainWindow by setWidget() method.
QMainWindow is made for just a mainwindow but it is not prevented from being used as a subwidget.
2.QToolBar can be attached to the main-subwindow by addToolBar() method.
3.The subwidget-mainwindow can naturally have its own QToolbar.
If you don't want to use QMainWindow as the widget of its QDockWidget,you can attach the QToolBar as a child widget of QDockWidget. But The toolbar is not movable as QMainWindow's.
I think you want to add QToolBar and use it as QMainWindow.
So I recommend that you set a QMainWindow as the widget of QDockWidget.And you attach any widget you like to the mainwindow after that.

Opening a Widget created in Designer, from a Main Window

I’ve created 2 .ui files, one is a main window, the other a widget. Designer generates the 2 .header files each with QT_BEGIN_NAMESPACE around the class declaration.
The problem is, what works in opening my main window, does not work in opening the second widget window.
To display my main window, I created a class that inherits from my .ui file:
class myWindow: public QMainWindow, private Ui::uiClassWindow
setupUi(this);
That opens fine, so then to open the second widget window, I declare a generic widget object and then save it with a pointer to my Widget Ui header file:
QWidget newWidget;
setupUi(newWidget)
But setupUi resolves to my Main Window header file… How do I tell it to use the Widget’s setupUi?
Is there a better way to go about this?
Up to my knowledge, for setupUi function is defined in Ui namespace. You need to give scope (Ui) for other widget too.
The setupUi() method is created by uic from your UI file, and it's different for each compiled UI.
In your myWindow, you inherit from Ui::uiClassWindow and can use its setupUi() method without qualification. You'll need an instance of a different UI class for your newWidget:
auto widget_ui = new Ui::myWidget;
QWidget newWidget;
widget_ui->setupUi(newWidget)
You could delete the widget_ui immediately if you wanted - but usually, you'll need to keep it in order to access the children it has now created in newWidget.

Context Menu of TableView in Child Widget

I am programming in C++ in QT and trying to make a UI with dynamic tabs having tables inside each of them. For doing the same, I had my TabWidget in the main window, and another widget with just the tableView. As the tabs are dynamically being added to the main window by a button click, I make a new object of my widget and put it in that.
I also have another version of the application in which there are no tabs, just a tableView in the main window.
I am unable to open the context menu in the former case, while it works perfectly for the latter.
I am using the signal "customContextMenuRequested" in both the cases. Don't understand what I need to add for it to work when the tableView is in a child widget.
Some help please?
Thanks already!
Did you check that nothing is involving QAbstractScrollArea, it's possible that in this case it would signal/slot as expected.
This signal is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu, and the user has requested a context menu on the widget. The position pos is the position of the context menu event that the widget receives. Normally this is in widget coordinates. The exception to this rule is QAbstractScrollArea and its subclasses that map the context menu event to coordinates of the viewport() .

How do I add a QWidget designed in Qt Designer inside a designed QMainWindow?

I started developing an application, I designed a empty QMainWindow wit just the menu bar. and created two new QWidgets and designed the features of my application on each.
Here is the thing: How do I add and interchange the last two QWidgets inside my QMainWindow?, so QMainWindow will show one set of features of my application at once.
Perhaps you are looking for QStackedWidget
(from Qt: The QStackedWidget class provides a stack of widgets where only one widget is visible at a time.)
You can find it insde the Containers group
Add a StackedWidget into your MainWindow, then
Directly edit the widget at present page by Qt designer(use the upper-right arrows to switch between differetn widgets). Notice that
in StackedWidget you have to create another signal sender, ex: a
combobox to decide which widget should be shown), or
If your custom widget contains some customized and hand-crafted
funcationalites that Qt doesn't have, you might need the widget
promotion. (otherwise, skip this)

How to detect if focus shift from a widget inside a QWidget to one outside?

I'm programming in Python using Qt with PySide and I have custom QWidget defined in a file called editor.py which is inserted in my ui in windowUi.py using the promotion method in the Qt Designer.
The custom QWidget class defined in editor.py doesn't do much besides using Elixir to edit items in a sqlite3 database and importing a ui file (editor.ui). Inside editor.ui there are a couple of QLineEdits and QDateTime widgets.
This widget is originally hidden in the main window and showed when needed. So far so good, but the problem is that I cannot make it hide when not needed. I decided that the widget is not needed when the user clicks anywhere else in the main window that is not the editor widget imported, that is, focus shift from the QWidget.
I looked upon this question: QWidget focusOutEvent not received and realized that the QWidget is really not getting focus.
If I call setFocusPolicy(StrongFocus) on it then I can make it hide if, and only if, the user clicks on the QWidget background (not on any widget inside it) and then clicks outside.
The question is then, how can I make it such that when the user clicks outside of this widget, shifting focus from any QLineEdit or QDateTime that's inside it to something else, the QWidget then hides itself?
Doesn't QApplication:::focusChanged ( QWidget * old, QWidget * now ) do what you need? You can check if QWidget *now is one of your QLineEdits/QDateTime or not (f.e. by going up by the QObject::parent)
Simply connect to this signal before showing and disconnect after hiding.

Resources