Implementing drag and drop with QT 4.5 using QT Creator environment - c++

We're about to commit to Qt and C++ (sigh) to do some cross-platform development. The latest version of Qt 4.5 seems very nice as does the QT Creator IDE, which although simple compared to other IDEs, is a good way to get started.
I'm trying to understand how to do drag and drop into QT widgets from the "outside" world. As far as I can tell from the documentation, you're supposed to subclass a widget that you want to have respond to drop events and override some methods (the dragEnterEvent and dropEvent member functions) for that widget.
But if I use the Qt Creator tool, I don't seem to have any access to the classes of the widgets that I have created using the GUI form builder and so I can't subclass them.
WHat's the secret?
Thanks in advance,
D

Someone on my team figured it out ---- turns out there is an option to "Promote" a widget, meaning you can subclass it to something else and then override the needed methods with no pain.
Seems to me it would have been more obvious if it said "Subclass widget..." rather than "Promote" but that's OK.
This QT Creator is a very nice piece of work.

I've never used QT creator environment, but I assume it spits out code afterward. Can you edit the code it spits out?
If you subclass the classes in a separate file, it shouldn't get overwritten when you rebuild your app with QT creator environment.
I suppose this is really a question for the QT creator forum though, sounds like a problem in THEIR user interface.

Related

Qt UI class (Clion)

I've been studying c++, but only for console application.
I'm trying to introduce myself in GUI applications with c++, since I'm working with CLion, I've noticed a Qt UI class, but I don't know how to use it.
Anyone know how to use this class, for create GUI apps (create events,...,).
I don't know if it's mandatory to install some extensions or not.
I'm a little bit lost in this area, so if anyone could help me, I would be grateful to you.
Well, maybe the Qt Creator IDE can be of help since the design of UI can be just dragging can editing so it is very friendly to starters.
Of course, you can edit the code Qt Creator generates.
As a matter of fact, I started working on Gt UI by dragging the components and got amazed at the 'talent' of me creating a simple GUI app in a minute!
Below is an example UI interface of Qt Creator. Have fun with Qt!

Qt 5 Mac toolbar woes

I am trying to make a very simple toolbar in a QMainWindow on a Mac, with Qt 5.2.1, with not a single satisfaction. I was using still Qt 4.8 but I thought I'd give Qt5 a try.
As QtMacExtras are concerned, I don't find the native toolbar class anymore, except in private headers. So I tried a simple QToolbar.
I have a very basic and stupid toolbar:
setUnifiedTitleAndToolBarOnMac(true);
toolbar_ = new QToolBar(this);
toolbar_->setMovable(false);
toolbar_->setFloatable(false);
toolbar_->addAction("h");
toolbar_->addAction("w");
toolbar_->addSeparator();
toolbar_->addAction("f");
As you can see, well, it isn't acceptable.
Is there any chance, by some arcane and weird means, that I could have a nice unified, or better "native look and feel", toolbar on a mac?
QMainWindow::addToolBar(QToolBar *toolbar) should do it.
The NSToolbar does not form part of the Qt widget hierarchy, so your admirably idiomatic conference of parenthood from the main window to the toolbar is working perfectly to specification, yet is obviously dissatisfying.
However, as the Trolls say in the QMainWindow documentation:
"You add a toolbar to a main window with addToolBar()."
Here is the effect as shown in rsync client for Mac.
I don't have my mac with me otherwise I would give this a quick test. I was surprised when browsing how many bugs are still being reported for Mac. I did find this article, which does appear to have better looking toolbar.

Qt5 - Porting a Video Player from 4.8 to 5.1

I'm working on migrating my code from Qt 4.8.4 to Qt 5.1.1 and seemed to have run into a peculiar problem. Previously I was using the Phonon library's video widget to allow users to interact with the video. Since Phonon is no longer supported I was looking for a way to replace my Phonon widget via the Qt Designer but I found no replacement widget available. Does anyone know how we're supposed to go about porting this functionality?
If anyone has any insight, it is appreciated as always!
All the other answers provide very useful information, but since I was looking for a specific and functional implementation, I'm guessing people that stumble on this may be looking for one as well, so I'll post my code along with my thought process and a bit of background info of my setup.
videoWidget = new QVideoWidget;
player = new QMediaPlayer;
verticalLayout->addWidget(videoWidget);
player->setVideoOutput(videoWidget);
player->setMedia(QUrl::fromLocalFile("Resources\\videos\\example.m4v"));
videoWidget->show();
player->play();
So to go through this, basically in my project I have a widget that is a part of a stackwidget structure on which I have some QWidgets like playback control buttons, labels, etc, and I previously had my Phonon video player. I really really didn't want to generate it all programatically so I managed to find a solution.
The above code was located in this container widgets constructor. In the designer I have made a simple, empty verticalLayout, positioned and sized it to my liking. This allowed me to embed the QVideoWidget despite not having a interactive QWidget in the Designer. videoWidget, and player are declared as private in the container widget's header file.
You've already noted there's no more Phonon
Now what you've got is QtMultimedia and the QtMultimediaWidgets. If you want them available to your project and you're using Qmake, then change your QT line in your .pro file from something like:
QT += widgets
to:
QT += widgets multimediawidgets
That won't do anything about the designer interaction, though. I think it's probably a matter of no one having done the work to make a custom widget extension for QVideoWidget.
If that's correct, then if you want to place a QVideoWidget into your form via Qt designer you will have to use "Widget Promotion". Just put an ordinary QWidget into your layout, and then right click on it in the form and pick Promote to ...
As for the specifics of porting Phonon abilities to the new widgets, I dunno what's covered and what isn't. No answers here yet, either:
How to port Qt4.6 Phonon based media-application to Qt 5.1?
It seems that although Qt itself no longer includes Phonon, there is a Qt5 port available as an external library:
http://community.kde.org/Phonon/Releases/Core/4.7.0
Alternatively, you can switch to the new QtMultimedia APIs:
https://qt-project.org/doc/qt-5.1/qtmultimedia/videooverview.html

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)

QT widgets vs QT GUI

I'm new at QT, actually I didn't start yet but I'm intending to create a xml file (markups and nodes and everything .. ) from a QT user interface.
For example the user can find in my interface text fields where he can insert an employee name, id and age and my program will turn that to an employee node in my output xml file with its attributes name , id , age. so since I'm coding with c++ I wanted to create this interface as a QT GUI but I found that QT creator provides a project named QT Widget with the same option.
So I'm kind of confused now and I don't know what's the difference between them so I can chose.
I will appreciate any help from guys.
If I have understood your question correctly: a Qt Widget is a tiny element, one of the many items in a gui (buttons, comboboxes are all widgets). The Qt Widget, project type is for creating one, which you can use in a separate projects interface.
A Qt Gui is more likely the project type you want, that will allow you to drag in many widgets to create your 'interface text fields'.
You would use a Qt Widget project type if you need to do more advanced customization or create your own text field control.
Do you mean Qt Quick vs. Qt Widgets?
Qt Quick is a more recent type of Qt GUI which is created from a declarative markup languages known as QML. The QML source is interpreted at run-time as opposed to Qt Widgets which are compiled from C++ source code into native executable code. In addition to QML, Qt Quick uses inline Javascript for scripting the UI, but it can be (and usually is) interfaced from C++ for more complex processing.
EDIT: Qt Quick is also very much touch-oriented (at least at its current state) whereas Qt Widget GUIs provide a much richer set of UI elements. So if you are making a desktop application, you might want to leave Qt Quick alone.