In the past I've had some experience of Qt in C++. I've now started using PyQt, and finding it a bit bewildering. There doesn't seem to be any definitive source of documentation, apart from a small amount at Riverbank. I guess the first thing I'd like to know is that there's an initial hump with PyQt, and it does get easier. [Edit: The main problem I was having was due to a typo - init not __init__. I'm not finding it so hard now :P]
The PyQt docs talk about new style signals and slots for PyQt, as well as old style. They suggest that the new style is better, but I was wondering if that is what most users of PyQt do.
Yes, that is. New syntax is more clear, so why not?
Note, that when you trying to connect slots by name, you must call connectSlotsByName explicitly, since there no preprocessor that will work before execution and connect them.
PS: Beside, C++ syntax for signal/slot connnection is just ugly, and old PyQt syntax was pretty similar without any reason, so I was glad to see this change in PyQt.
PS2: There was the question here recently about this, check it out.
For reference see: PyQt Class Reference - it's quite comprehensive and goes into details with examples. Most of the examples are in C++ and although I'm something of a Python newbee, I didn't find it difficult to translate into Python.
If you're an experienced developer and starting to use Python and PyQt, this is a pretty good book - about the only full scale book on the subject that I know of: Rapid GUI Programming with Python and Qt.
Signals and Slots: I always use the new syntax for signals and slots in PyQt, which is simple and elegant - much more 'pythonic'. PyQt is great, but in many respects it's still very C++ like - the more they do to 'pythonate' it, the better.
Related
I'm trying to make QTcpServer start a separate thread for each connection. There's example code available that does just that: http://doc.qt.io/qt-5/qtnetwork-threadedfortuneserver-example.html
This code works by subclassing QThread. If I build my code based on that example, I'm pretty soon running into "QObject: Cannot create children for a parent that is in a different thread." warnings.
There is documentation from Qt that advises against subclassing QThread, because that is "doing it wrong": http://blog.qt.io/blog/2010/06/17/youre-doing-it-wrong/
So, my question is, if the Qt provided example code is directly in violation of the Qt recommended practice, is there an example of the correct way to do it? Any example I've found so far subclasses QThread.
Qt is a developing product. And, I seem, subclassing QThread is still considered as legal practice. It is just an older technique, so old examples use it.
Your problem seems to be the wrong usage of QObject(s) across different threads, rather than QThread subclassing. First try to find the code which issues the warnings.
I am making an interface for a free-and-open project I'm working on. I know that eventually I'll need it to do some heavy lifting with 3D graphics, so I'll need access to the GPU. I stumbled upon the Qt framework and its use of OpenGL. Their cross-platform properties are very appealing to me, and I've learned a lot about building run of the mill / plumbing kinda stuff on Qt, but using OpenGL is more challenging than I'd have hoped.
I've rooted around in several books and on Google for good tutorials, but they jump around, or don't seem to quite work when I try to implement them. The examples presented in the IDE have a lot of bells and whistles, and almost none of it is explained. When I tried implementing the example from "C++ GUI Programming with Qt 4", it didn't compile properly. There are a set of somewhat famous "NeHe" tutorials on OpenGL, but they are not implemented with Qt and the companion series on made by "The Independent Qt Tutorial" is very brief.
I would be very grateful for an example that goes very slowly and introduces Qt and OpenGL together. There is a blog entry that made things a lot clearer when I read it from a man named Daniel Eder. But he hasn't actually written code in that part of the discussion.
Specifically, if I could just get something really simple working in Qt like a cube I could rotate or maybe something more sophisticated like a sphere, I'd have enough traction to understand other tutorials. I definitely want to do the coding myself, but I'm having trouble even getting to the place where I can fool around, haha.
Here are some places with tutorials and examples:
There is also a Wiki in the Qt Dev network that you may find useful. I particularly suggest Wesley Stessens's blog that helped me personally a lot.
Nehe website provide a good tutorial where you can find some of the examples given ported to Qt.
There are also many QtOpenGL examples in Qt Creator/SDK that can be handy. I strongly suggest you take a look at them as they are very simple to understand and to play around with.
With that my next step was to read these two books:
C++ GUI Programming with Qt 4, 3rd edition
OpenGL Programming Guide: The Official Guide to Learning OpenGL, 7th edition
I have also been very happy when looking for help in the related Doc's/Reference manuals, etc.:
Qt Documentation
QtOpenGL module
OpenGL Documentation
At this level, you may solve your implementation problem straight away : )
Good luck.
I have to port a project from Borland C++ Builder 5.0 under Windows XP to Qt 4.7.1 using g++ under Windows 7/mingw. The libraries and command-line utilities are done, and now I have to tackle the GUI applications, which use Borland VCL.
Can anybody recommend any tools or libraries to make this task easier?
Does anybody have any experience of this?
Edited to add: Well, I took the bull by the horns, and implemented the GUI from scratch. And I have to say, the commentators were right: I can't see any way of using the existing Borland GUI to ease the process.
There are several big differences between VCL and Qt that will make an automatic conversion process quite difficult.
Qt uses signals and slots and inheritance where VCL uses events.
VCL components use absolute coordinates and Qt uses layouts. Of course, you could use absolute coordinates also with Qt, but the GUIs would be quite awful then.
VCL's TListBox and TTreeView classes are quite different from Qt's View and Model classes (although you could use QListWidget and QTreeWidget instead).
I guess it is much faster to design totally new GUIs with Qt than to create even a mediocre VCL-to-Qt converter. And the code will be much easier to maintain. I suggest that you take one VCL form of medium complexity and recreate that with Qt. After that you can make an estimation of the total recreation work. Also you will have a better understanding about the feasibility of a conversion tool, which you most probably would need to make by yourself.
Someone has written a tool to convert dfm's to qt ui files:
http://sourceforge.net/projects/dfm2qt4ui/
Its has a few small bugs but it can save several hours of time porting form designs. In some cases redesigning specific forms is preferable - but in many cases, having labels and roughly equivalent controls positioned for you saves a lot of point-and-click action.
I agree with the current consensus that automatic conversion from VCL to QT is not a good idea because the concept behind both is very different, and you are much better off learning "the QT way" and using that from the start.
However there is one major step that nobody has yet mentioned: refactoring! Before starting, make sure you refactor the original forms to remove as much business logic as possible and leave only what is really GUI code. It depends on how good your architecture already is of course, but the VCL designer tends to encourage putting as much as possible in forms (even going as far as having invisible "data forms" with non-visual components!), so you often find a lot of stuff in the form that shouldn't be there.
everybody, I'm beginner in programming, I've just finished my course on C++, I want to train my skills and try to write some programs on C++ with graphic windows (not console programs, I did it during all my course), I want to start working with QT, is it good idea, or I need to begin with something simplier, thanks in advance
Qt is, among other things, a great framework for developing applications with GUIs. The only way to know if it is appropriate is to try it out. I suggest going through this Qt tutorial, which involves creating a simple game.
To begin with Qt is an excellent idea. Qt is already simpler.
You should begin with these official tutorials: http://doc.qt.nokia.com/4.7-snapshot/tutorials.html
GUI program is a big change from a console app - the difference is that you don't know when things are going to happen and in what order.
But Qt is a very good place to start, the signal/slot mechanism is much clearer (IMHO) than the hidden macros of MFC or some other gui toolkits.
I've given up on actually trying to make it go faster.
My biggest problem is that when I'm inserting the html, the application slows down to a crawl.
I have a progressbar, and I'm calling
QCoreApplication.processEvents()
(I'm using pyqt, by the way)
Can I put insertHtml() into a different thread, so I don't have an unresponsive interface?
How would I go about that? I've looked into QThread and QThreadPool, and I'm not quite sure where to begin.
I had this problem as well, here are a few things I did to make it faster:
TxtBrows->setAcceptRichText(false);
TxtBrows->setContextMenuPolicy(Qt::NoContextMenu);
TxtBrows->setOpenLinks(false);
TxtBrows->setReadOnly(true);
TxtBrows->setUndoRedoEnabled(false);
This should get rid of unneeded overhead.
Also when inserting large amounts of text its good to turn off screen updates:
setUpdatesEnabled(false);
TxtBrows->append(SomeBigHTMLString);
setUpdatesEnabled(true);
This was recommended somewhere in the Qt documentation but I can't find the spot just now.
[Edit]
I stumbled across the spot in the Docs (just in time for them to be outdated by QT5 grinn) http://qt-project.org/doc/qt-4.8/qwidget.html#updatesEnabled-prop
In GUI applications, the main thread
is also called the GUI thread because
it's the only thread that is allowed
to perform GUI-related operations.
-- from the Qt Docs
So, no. Unfortunately you cannot perform that operation in a thread.
Edit: Technically, it is possible. I just wrote a short snippet that did so, however using Qt GUI objects in that way is highly unsafe.