I'm trying to call a Qaction using design in my .ui file. I can't find a way to call it. In button we can call it using code like below
self.window.mybutton.clicked.connect(self.startmyfunction)
how about in Qaction. Please see screenshot below on my design about Qaction
ui image of Qaction
I got it now, we can just easily use triggered.connect
For example:
self.window.mymenuhost1.triggered.connect(self.myfunctionexample)
Related
I am having a project with a Qwidget form and the Mainwindow form, this Qwidget form does not have it own class, but I want to call the form from the button click event of the Mainwindow.
I just cant figure out how I can make it work, any help is appreciated
You can create user interfaces dynamically at runtime using QUiLoader class.
See https://doc.qt.io/qt-5/quiloader.html .
Maybe that's what you are looking for.
I'm very new to Qt, and I need to simulate a click using the QTest Namespace and QTest::mouseClick. My problem is I would like to click a QMenu entry, defined as a QAction, but the mouseClick function doesn't allow me to pass this as an argument (only QWidgets or QWindows).
What could I do here?
You may use another way such direct triggers of your QAction's as far as you have them:
qAction->trigger();
This should have the same impact as mouse clicks in testing purposes.
A QAction doesn't have any UI itself, so it can't be clicked.
It can, however, be plugged into several UI components, e.g. in a QMenu or a QToolBar, which can be clicked.
So if your tests needs to simulate some user interaction, you simulate it with the UI portion created for the action, e.g. the respective tool button on the toolbar, or entry in a menu
I have just completed my first Qt 5 project and I want to add an About dialog. Is there a default, and how do you use it? If not, what's the best way to add one and edit it and display its content using the Help menu action? Any help would be appreciated. Thanks in advance.
The way to add a dialog to your project depends a little bit on what development environment you are using. You can always use QtDesigner (part of Qt) to design your About dialog and then add it to your project. To display it, just call the exec() function of the QDialog class in a slot connected to the triggered() signal of your About QAction object.
I have designed a GUI through Qt creator on Linux. This design consists of some fields, text edit and some push buttons.
When I press on the push button I want to display another window. Is there any GUI option for this or any hard code?
You need signals and slots.
You have to connect the clicked signal to a custom slot, created by you, of your main widget.
Corrected Code, based in the comments of Patrice Bernassola and Job.
In the class definition (.h file) add the lines:
Q_OBJECT
private slots:
void exampleButtonClicked();
private:
QDialog *exampleDialog;
The macro Q_OBJECT is needed when you define signals or slots in your classes.
The variable exampleDialog should be declared in the definition file to have access to it in the slot.
And you have to initialize it, this is commonly done in the constructor
ExampleClass::ExampleClass()
{
//Setup you UI
dialog = new QDialog;
}
In the class implementation (.cpp file) add the code that does what you want, in this case create a new window.
void ExampleClass::exampleButtonClicked()
{
exampleDialog->show();
}
And also you have to connect the signal to the slot with the line:
connect(exampleButton, SIGNAL(clicked()), this, SLOT(exampleButtonClicked()));
Your question is somehwat basic, so I suggest to read a basic tutorial, in this way you can make progress faster, avoiding waiting for answers.
Some links to tutorials that were useful to me:
http://zetcode.com/tutorials/qt4tutorial/
http://doc.qt.io/archives/qt-4.7/tutorials-addressbook.html
on click event of button you create another widget and show.
another option is Stacked widget, http://doc.trolltech.com/4.6/qstackedwidget.html
when i add the widget to the main window, by default the action menu item will be present,
how to remove that?
menuBar()->setVisible(false);
verAction = new QAction(tr("&Version"),this);
menuBar()->addAction(verAction);
connect(verAction, SIGNAL(triggered()),this, SLOT(displayVersion()));
displayAction = new QAction(tr("&Display"),this);
menuBar()->addAction(displayAction);
connect(displayAction, SIGNAL(triggered()),this, SLOT(displayMessage()));
exitAction = new QAction(tr("&Exit"),this);
menuBar()->addAction(exitAction);
connect(exitAction, SIGNAL(triggered()),this, SLOT(close()));
Thanks
If you want to hide an QAction and display it when you need it, you can use the setVisible function.
If you want to remove the menu bar from the QMainWindow, you can use the QT_NO_MENUBAR preprocessor to remove all uses of a QMenuBar. If you are not using facilities provided by QMainWindow, maybe you can use a simple QWidget as main window in your application.
[Edit]
If you want to hide QActions at runtime, you will find them as member of the QMainWindow's UI. For example if you have a QAction named actionTest, you will access it like that: this->ui->actionTest->setVisible(false);
I know what you mean... you want to HIDE the DEFAULT CONTEXT MENU "Actions"....
You can do this in the Design section (not in code).
Then you see your Object-Stack on the right side like
MainWindow QMainWindow
centralWidget QWidget
webView QWebView
Now go to the property editor below...search for "contextMenuPolicy" and change it from "DefaultContextMenu" to "NoContextMenu" for every component if necessairy.
In order to remove the default context menu with the label "Actions" the following code may be used:
// Remove context menu from the all widgets.
QWidgetList widgets = QApplication::allWidgets();
QWidget* w=0;
foreach(w,widgets) {
w->setContextMenuPolicy(Qt::NoContextMenu);
}
Essentially, the same as the Joel's answer, but the code version :)
(Code taken from QFriendFeed sample from forum.nokia.com)