Qt - setupUi( ) [duplicate] - c++

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Qt - initializing the form
I tried to look for a description for the setupUi() method but couldn't find especially in the Qt documentation.
What does this method do? For example, if I write in a class setupUi(this), what will this do? What does setting up a user interface mean at the end?
Thanks.

setupUi() creates the actual instances of widgets for you. A form that you create in QtDesigner is stored just as XML file. So to be able to build the actual "window" with all the elements that you put on it in QtDesigner and display it in your application, setupUi() is created for you automatically by UIC (UI compiler - a Qt tool) so you don't have to do that manually. All the properties that you set in QtDesigner and all elements you put there will be "translated" in C++ code like this:
QLabel *label1 = new QLabel(tr("Start"), this);
QTableView *view1 = new QTableView(this);
...

Related

Which Class should inherit a QMainWindow? [duplicate]

This question already has answers here:
What's the difference between QMainWindow, QWidget and QDialog?
(2 answers)
Closed 3 years ago.
I am new to Qt and this is probably a very stupid question. I am working on a Project with several sub-projects in Visual Studio (C++). The whole project has a single User-interface (UI), and for that, the Qt is used.
Among all the sub-projects, only 2 classes are inherited from QMainWindow.
As a newbie, I am wondering, which classes should inherit QMainWindow?
Just like any other class, QMainWindow should be inherited when one needs the functionality it provides (or rather to customize that functionality). Specifically QMainWindow (vs. just QWidget) provides built-in menu bar, dockable toolbar(s), status bar, dockable subwidgets, a way to save and restore their states, and some other niceties one might want for a full GUI application. But any QWidget can be a top-level widget (the "main window" of an app) so there is no requirement to use QMainWindow as the top-level widget. If one doesn't need/want any of those extras, there's no reason to inherit/use QMainWindow.

Prevent Qt from generating shortcuts? [duplicate]

This question already has an answer here:
How to disable automatic mnemonics in a Qt application on KDE?
(1 answer)
Closed 7 years ago.
I am creating a QPushButton as follows, with no explicit shortcut (&):
testButton = new QPushButton(tr("Start"));
I can correctly read the button's text just after its construction:
testButton.text() returns 'Start' (without single quotes)
Then I create a signal/slot connection:
QObject::connect(
testButton, &QPushButton::clicked,
this, &Dialog::actionRequest
);
In the Dialog::actionRequest slot, testButton.text() returns '&Start', as if a shortcut had been created "somewhere", under the hood.
If I name the button 'Foo', the same phenomenon occurs, but testButton.text() would return 'F&oo'. That's because I already have a "File" menu, that has an explicit shortcut -- created as:
fileMenu = new QMenu(tr("&File"), this);
and, possibly, Alt+O would be the next "available" shortcut?
I do not want this implicit shortcut creation for my QPushButton. What's the solution to this problem? I'm running Fedora 22 ( with plasma 5), with stock qt 5.5 packages.
There is a default QShortcut object associated with menu options, buttons, etc. Unfortunately there is no way to directly access and disable them after creation that I know of.
You can attempt to call the Qt global function qt_set_sequence_auto_mnemonic(false); to disable this behavior dynamically but I'm not sure if it's available on every platform.

Qt Creator 2.8.1 Qt 5.1.1 Qt Designer Linux Show a new Form

I am a beginner with Qt - so hopefully this will be an easy question to answer. I have a reasonable amount of experience with C++, that part is not a problem
The purpose of my application is to do code generation, initially to make header & implementation files for classes. I quite like the Class wizard on Code::Blocks, but I reckon I can do a lot more.
I have a main Widget which has a tabWidget & some lineEdit's & some pushButtons. To preview what will eventually be in the file, I have created a new Form, with a TextBrowser in it. The new Form entry appears in the .pro file.
I would like to have the new Form displayed when I press the pushButton, & I am intending to write text in the TextBrowser based on the contents of the lineEdit's in the main Widget.
I have been looking through the documentation all afternoon, but most of the examples show either a main widget or a Form by itself. I have seen the example of the Class Wizard (which is nearly what I want to do), but I would rather a tabWidget interface. Being able to open a Form from a button is a pretty basic thing to be able to do.
For some reason, the examples page in my QtCreator help doesn't show any examples - previous versions had heaps of examples. Not sure why that is.
Do I have my terminology mixed up - should I have a Dialog rather than a Form? Not sure what the difference is.
Apologies in advance if all this is in the documentation somewhere, I seem to spend hours trolling through it, so maybe someone could provide some links - that would be great.
Suppose the new form you created along with with header and cpp file is mynewform.h, mynewform.cpp and mynewform.ui
Now include mynewform.h in your mainwindow class,
and create an object of the class
mynewform myform;
In the clicked slot of pushbutton just type:
myform.show();
or
myform.exec(); //(if you want a blocking call)

How do you programatically update the UI in Qt?

I'm fairly new to Qt, and am trying to wrap my head around signals and slots. Though I've figured out how to create custom slots, I've yet to figure out how do update the GUI from my C++ code. I realized that the entire UI, which I created in the designer, is only written in what appears to be XML based UI code. Do I have to handwrite my own Qt C++ UI in order to update the interface, or can I somehow use C++ to update the XML based UI? I'm just looking to add a widget to the main form on a button click. Any help is appreciated. Thanks!
To add a widget to a form you can simply do
ui->layout()->addWidget(new Widget);
XML is used by QtDesigner as a mean to create, update, and persist your GUI, enabling a visual approach to development, but in the end you can build your application entirely without it.
you dont havfe to update the xml of UI . you can inherit the ui into your own class using setupUi
http://crpppc19.epfl.ch/doc/qt4-doc-html/html/qwidget.html#setupUi
now in your C++ class you can update the widgets like changing label text, lineEdit text, setting up spinbox value using Signals & slot.
That xml is used by Qt designer and outside of designer it's only used by uic as a source to generate C++ code, so everything that you do in the designer end-up as C++ code, so it can be updated (or done completely) in C++ code (you can look into the code and see that you most likely have a member called ui that is most likely a pointer, but it can also be an instance of a C++ class that is generated for you by uic, and trough that ui member you can access the widgets and layouts that you created in the designer).
A generic resource that i recommend you to read can be found here and then (if you still can't figure it out) ask a specific question about what exactly are you trying to achieve.
LE: that link contains 3 methods to use the generated code into C++ code, don't try all of them into your working project, you may choose one method or use the default one (ui member pointer)

Floating/Embedded QDockWidget in a QWidget (KXmlGuiWindow's CentralWidget designed in QT Designer)

I'm just trying to get into QT (and KDE) program, and have hit a snag trying to add a floatable, draggable QDockWidget to the .ui file (based as a QWidget) that is embedded into my KDE 4 program.
This is all from the basic template provided by KDevelop 4, so while I understand what's going on, I just don't know the best way to change it.
Here's the deal: main.cpp spawns a new AEmpire window, which starts the whole show off:
AEmpire::AEmpire()
: KXmlGuiWindow(),
m_view(new AEmpireView(this)),
m_printer(0)
{
// tell the KXmlGuiWindow that this is indeed the main widget
setCentralWidget(m_view);
setupActions();
setupGUI();
}
When a new AEmpireView(this) is created (which inherits from QWidget) and is assigned to m_view, this constructor is called:
AEmpireView::AEmpireView(QWidget *)
{
ui_aempireview_base.setupUi(this);
settingsChanged();
setAutoFillBackground(true);
}
So, when I am editing the ui to my program in QT Designer, I'm actually editing the AEmpireView_base ui file, which is a QWidget. It just represents the main view of the KXmlGuiWindow (derived from QMainWindow) and is loaded at runtime.
So, how do I add floatable, draggable QDockWidgets into my main application? Is designing them separately, and add them to the UI the best option? Or maybe removing the entire AEmpireView class, and making my ui file directly represent a KXmlGuiWindow object to be loaded by the AEmpireClass?
Or am I totally overlooking something obvious? Thanks for reading!
I would design the QDockWidget contents as separate UI files. Then create them and stick them into the QDockWidgets in the AEmpire constructor.