i have a Qt project with mainwindow (GUI Application)
how can i communicate with my main via my form?
for example
increase variable (which there's in my main) while button a click in my form
i have make a project in C which its in console application. works fine, and now i make another project which contains ui form with same code. and i want to make communication between form and main function
You can use the Qt signal/slot functionality.
Example:
// on mainwindow.cpp. Delcare onButtonClicked as a slot() on the header
void onButtonClicked()
{
++myVar;
}
connect(myButton, SIGNAL(clicked()), this, SLOT(onButtonClicked());
Reference docs.
Related
I have been reading though a couple of examples and post but I just cannot figure out how to add a shortcut to my custom context menu. My GUI has several elements. One of them is a treeView. For the elements in my treeView I would like to have a custom context menu.
My first approach was according to this tutorial here. The context menu itself worked but the shortcuts cannot work if you create the actin within the show function.
So my second approach was according to this tutorial. But still my shortcuts do not work and if I use the context menu all actions are called twice...
Since I did not find a tutorial or code example, which matches my case, I hope that someone here can explain to me how this is correctly done in theory. Adding a shortcut to an action for a custom context menu.
Where do I have to declare my action?
What needs to be the parent of the action?
On which widget do I need to call addAction?
Thanks for any hints.
Another way is to add your action also to the parent widget (or main window widget). As mentioned in this reply, adding the same action to multiple widgets is fine and it's the way QActions are supposed to be used.
Example with custom HtmlBrowser class deriving from QTextBrowser:
Ctrl+U shortcut works for this code:
HtmlBrowser::HtmlBrowser(QWidget * parent) : QTextBrowser(parent)
{
viewSourceAct = new QAction(tr("View/hide HTML so&urce"), this);
viewSourceAct->setShortcut(tr("Ctrl+U"));
viewSourceAct->setCheckable(true);
parent->addAction(viewSourceAct);
connect(viewSourceAct, &QAction::triggered, this, &HtmlBrowser::viewSourceToggle);
}
and Ctrl+U shortcut does not work with this code (same as above, but without parent->AddAction(...)):
HtmlBrowser::HtmlBrowser(QWidget * parent) : QTextBrowser(parent)
{
viewSourceAct = new QAction(tr("View/hide HTML so&urce"), this);
viewSourceAct->setShortcut(tr("Ctrl+U"));
viewSourceAct->setCheckable(true);
connect(viewSourceAct, &QAction::triggered, this, &HtmlBrowser::viewSourceToggle);
}
Curiously, parent in this case is another widget (tab widget), not MainWindow. Still, adding parent->addAction() helps. And, unlike your suggested answer, it works even when connecting action to simple methods, without slots. Works for me in Qt 5.15.0. Not quite sure why it works, though. Perhaps, the widget the action is added to must be permanent for shortcuts to work? Looks like a bug in Qt.
Thanks to Scheff's hint I got it working. I do not now if this is really the correct way but this works for me.
The action needs to be declared in the constructor of your GUI class (e.g. MainWindow):
actionDel = new QAction(tr("delete"), this);
actionDel->setShortcut(QKeySequence(Qt::Key_Delete));
connect(actionDel, SIGNAL(triggered()), this, SLOT(actionDel_triggered()));
The triggered signal needs to be connected to a slot. Hint: if you create the slot do not use on_ACTIONNAME_triggered, this will interfere with the designer and cause a connection error.
Next add the action to a custom menu
fileContextMenu = new QMenu(this);
fileContextMenu->addAction(actionDel);
And to the widget
ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->treeView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showDirContextMenu(QPoint)));
ui->treeView->addAction(actionDel);
All in the constructor of your GUI class.
To show the context menu use the following code in slot used in the above connect:
QModelIndex index=ui->treeView->indexAt(pos);
// Here you can modify the menu e.g. disabling certain actions
QAction* selectedItem = fileContextMenu->exec(ui->treeView->viewport()->mapToGlobal(pos));
If you do not have a slot for an action, the action can be also handled in the context menu slot, but this does not work with shortcuts!
if(selectedItem == actionOpen){
on_treeView_doubleClicked(index);
}
I'm trying to do a very simple Qt Quick application.
Qt Creator automatically creates a MainForm.ui.qml file, in which I put my UI components, such as a TextField.
When the content of this TextField is changed, I want to call a invokable method of a C++ QObject.
Here is what I did :
TextField {
...<snip>...
onTextChanged: {
var Result = myCppObj.doStuff(text)
lblOutput.text = Result
}
}
When I start my application, it is working fine.
But the doStuff(text) call is underlined in red, showing the following message :
Javascript blocks are not supported in a Qt Quick UI form (M223).
And I cannot edit my UI with the designer tool any more.
Any idea why and how to solve this ?
I have an Mfc application where I want to open a Qt modal window, while the Mfc application is still running behind.
To start a Qt application from an Mfc, I got the how to there. This page creates a Dll but my solution creates a static library instead.
As for creating a Qt modal window with main application running behind, it looks easy.
My problem is that it looks like that I cannot combined both solution.
The following code starts the Qt window, but I strongly suspect that I am blocking my main thread and main thread is not able to receive events.
void QtMfcFacade::startDevicesConfigurationWizard(HWND hWnd)
{
QWinWidget win( hWnd );
win.showCentered();
DevicesConfigurationWizard devicesConfigurationWizardUI(&win);
devicesConfigurationWizardUI.exec();
}
exec function is probably blocking main thread
Following solution should not block main thread, but the Qt windows appears only for few miliseconds and disappeared
void QtMfcFacade::startDevicesConfigurationWizard(HWND hWnd)
{
QWinWidget win( hWnd );
win.showCentered();
DevicesConfigurationWizard devicesConfigurationWizardUI(&win);
devicesConfigurationWizardUI.show();
}
So, the question is, is it possible to use show function instead of exec function?
(This works if I am creating a Dll and calling the Dll in a worker thread from my Mfc application, but I do prefer using static library)
I did not read enough the walkthough.
It is said : "Since we want the dialog to be modeless we cannot create the QWinWidget on the stack, since it would be deleted when it leaves the scope, and all its children, including the dialog, would be deleted as well. Instead we create QWinWidget on the heap, using operator new"
So, to make my window non modal while giving control back to main thread, I should do this :
void QtMfcFacade::startDevicesConfigurationWizard(HWND hWnd)
{
QWinWidget *win = new QWinWidget( hWnd );
win->showCentered();
DevicesConfigurationWizard devicesConfigurationWizardUI = new DevicesConfigurationWizard (win);
devicesConfigurationWizardUI->show();
}
I am trying to move my QSystemTrayIcon module to a separate thread. I am getting the below error while running the application. The Initialize() method addAction causing the error.
QWidget: Must construct a QApplication before a QPaintDevice
My sample code snippets:
Declared the global variable
QMenu myMenu;
Member Variable in the header file
QAction* openFile;
In the constructor
openFile = new QAction(parent);
Initialize()
{
myMenu.addAction(openFile);
}
void myApp::run()
{
Initialize()
}
You must not use any GUI-related classes outside your main thread, i.e. the one QApplication was created in. Hence you cannot move your QSystemTrayIcon stuff in a separate thread. And no, there is no workaround.
You are approaching this problem backwards. The reason that "The TrayIcon menus are not displaying if some functionality is running at the background Menus will display after the process completion." is because you are blocking the GUI thread when you wait for things. Do not block the GUI thread by waiting on things. Most of Qt provides signals that fire when things get accomplished (when they succeed or fail). Those are the nonblocking APIs that you should be using.
How can I in code of the custom Qt widget know that it is currently instantiated in Qt designer?
Use case:
I build a complex custom widget that has several child widgets like QPushButton, QLabel etc.
As application logic require, when widget is created most of those sub component are not visible but in design time when I put it on a form I would like to see them.
To be able to play with style sheet at design time.
Currently what I get is a empty is only a result of constructor - minimal view (actually empty in my case).
What I am looking for is to be able to do something like
MyQWidget::(QWidget *parent)
{
....
if(isRunningInDesigner())
{
myChildWidget1->setVisible(true);
myChildWidget2->setVisible(true);
myChildWidget3->setVisible(true);
}
else
{
myChildWidget1->setVisible(false);
myChildWidget2->setVisible(false);
myChildWidget3->setVisible(false);
}
....
}
So what should I put in to this bool isRunningInDesigner() ?
From the Qt Designer manual:
To give custom widgets special behavior in Qt Designer, provide an implementation of the initialize() function to configure the widget construction process for Qt Designer specific behavior. This function will be called for the first time before any calls to createWidget() and could perhaps set an internal flag that can be tested later when Qt Designer calls the plugin’s createWidget() function.
Those are methods from the QDesignerCustomWidgetInterface plugin interface. In short: you tell the widget to behave differently when Qt Designer asks your plugin to create instances of your custom widget.