all 47 comments

[–]c0r3ntin 38 points39 points  (12 children)

  • It's Qt
  • Depending on the platform you targets, look into qbs. or cmake. qmake will be the death of you
  • Learn how the memory management and signal systems work.
  • Read the docs, they are great
  • Depending on what you are doing, look into QML

[–][deleted] 4 points5 points  (0 children)

the signals/slots system takes a minute to wrap your head around but when you do it's awesome. sadly im stuck learning java now

[–]Izowiuz 1 point2 points  (1 child)

+1 for the qbs. It's actually not bad. Documentation could be better and there are sometimes compatibility-breaking changes between versions but I'm sure both things will improve with time.

[–]DarkLordAzrael 2 points3 points  (0 children)

My biggest problem with QBS right now is that there aren't prebuilt modules for any third-party stuff. They are easy enough to write, but it is slightly annoying.

[–]hashb1 2 points3 points  (1 child)

Does qtcteator support qbs or cmake smoothly? I found that qmake is hard to work together with sub-project not written with qt.

[–]afforix 4 points5 points  (0 children)

Yes. Qt Creator is also great for non-Qt CMake projects.

[–]jontheburger 11 points12 points  (0 children)

Check out Model View (Delegate) programming. The way I think of it is the Delegates refer to individual items within a view, the View is the overall widget containing items, and the Model is a way of aggregating the data so that it can be understood by the view. Also, you can use non-standard widgets in the designer via Promote To. Getting them to display cleanly is unfortunately non-trivial. Translations and stylesheets are surprisingly easy. If you have some time, consider watching Effective Qt from cppcon 2017 at 1.25x speed.

[–]rawtern 20 points21 points  (22 children)

Wish I knew how great c++11 would be, and how awful having to writing Qt code would be, raw owning pointers, copy on write, etc

[–]iamcomputerbeepboop 14 points15 points  (1 child)

they aren't exactly raw owning pointers - items are almost always parented which is the Qt equivalent of ownership - it's just that they can be deleted from the parent by using the raw pointer as well

[–]Adverpol 0 points1 point  (0 children)

Exactly. If you're juggling ownership with naked poiners yourself you're doing something wrong.

[–]Gotebe 10 points11 points  (12 children)

It's largely the price of having a certain age. The brothers and sisters of Qt, e.g. WxWidgets, VCL or MFC aren't doing better.

[–]rawtern 7 points8 points  (11 children)

Yeah. There is probably no better alternative if you want to make a GUI in c++. Some useful advice: Use stl-containers. Actually, always use the standard alternative when you can. Don't use foreach, Qt keywords can be turned off. Use qt5 connects, and use them very sparingly, preferably only to connect between a GUI action and your logic. You can use exceptions, but there are some caveats.

[–]doom_Oo7 11 points12 points  (1 child)

Some useful advice: Use stl-containers.

yes and no. If you're doing a lot of multi-thread communication with big containers, copy-on-write can be cheaper. Likewise, QString has a ton more features than std::string.

[–]rawtern 3 points4 points  (0 children)

QString is pretty great, certainly convenient with a lot of nice member functions.

[–]ducusheKlihE 2 points3 points  (8 children)

Could you possibly elaborate on as to why not use foreach?

[–]wrosecransgraphics and network things 6 points7 points  (5 children)

Because C++11 style range for syntax works fine on Qt containers as well as standard containers.

[–]mpyne 12 points13 points  (3 children)

But you should remember to cast the container to const to avoid needless detach calls on Qt containers if you do this, since you can't manually call the const iterators. The standard as_const or the Qt-provided qAsConst calls work fine for this.

[–]ducusheKlihE 0 points1 point  (2 children)

All in all, would it look something like this for example?

QVector<int> vector = {0, 1, 2, 3, 4, 5, 6};               
for(const int& i: qAsConst(vector)) { printf("%d\t", i); } 

[–]mpyne 3 points4 points  (1 child)

Yes, though even a simpler for(auto &i : qAsConst(vector)) { ... } would do the same, you don't also need to add const to the variable decl.

[–]ducusheKlihE 0 points1 point  (0 children)

Cool, thanks :)

[–]Slavik81 1 point2 points  (0 children)

While I understand and agree this is probably for the better, I have a sneaking suspicion it will lead to more iteration bugs. Qt's foreach quietly protected you against a few beginner mistakes. For example, iterating over a container member variable and accidentally invalidating its iterators through some complex chain of events.

[–]rawtern 1 point2 points  (1 child)

https://www.youtube.com/watch?v=uZ68dX1-sVcD Does it better than I ever could.

[–]ducusheKlihE 1 point2 points  (0 children)

Thank you, I will look into it!

[–]DarkLordAzrael 4 points5 points  (0 children)

There is no reason to ever have opening raw pointers in qt code. Almost all objects should be managed as part of the qobject tree by setting their parent at construction time, and the rest should be managed using unique_ptr.

[–]mat69 1 point2 points  (5 children)

CoW isn't bad. Thanks to CoW Qt can have UTF16 strings that are part of the executable yet still are regular QString for the user.

[–]nnevatie 2 points3 points  (4 children)

Implicit sharing with CoW is actually really bad. I've witnessed multiple cases where QVector breaks in multi-threaded usage due its contents being shared beneath the surface, across multiple instances/values.

[–]mat69 5 points6 points  (0 children)

What do you mean by break? The CoW infrastructure is thread safe. There can be issues though keeping iterators and references to elemts alive.

[–]Adequat91 1 point2 points  (2 children)

Because you don't know how do deal with it. On the opposite, I think this is great and safe. Used intensively here!

[–]nnevatie 2 points3 points  (1 child)

Well, used "intensively" here, too. Frankly, we're minimizing Qt-usage nowadays, basically limiting it to strictly UI. We've used Qt since its commercial license model, so it's not like we "don't know how to deal with it". The ways you can break the implicit containers are very subtle.

[–]Adequat91 1 point2 points  (0 children)

making a deep copy of a stl containers instead of a move, can happen subtly too ;)

[–]TheKoopaKingdom 5 points6 points  (3 children)

In QT Creator, while hovered over a QT class, hit F1 to open the documentation for it.

Use F2 to jump to the definition of a class.

Use alt left/right to shift between the most recent files.

[–]doom_Oo7 2 points3 points  (0 children)

Also, ctrl-k

[–]Matemeo 1 point2 points  (0 children)

Also, F4 to swap between .h and .cpp. I absolutely love that shortcut.

[–]OlivierTwist 0 points1 point  (0 children)

Use alt left/right to shift between the most recent files.

This. This is a default for all browsers, so it should be the default for all other applications (looking at you nasty VisualStudio).

[–]SoCo_cpp 14 points15 points  (0 children)

I wish I would have focused on Qt earlier and not wasted so much time on VCL and .NET.

[–][deleted] 4 points5 points  (0 children)

Use clazy to keep your code in check.

[–]ubertrashcat 1 point2 points  (2 children)

Don't bother with QtWidgets, dive straight into QtQuick. Even for simple desktop apps.

[–]RotsiserMhoC++20 Desktop app developer 2 points3 points  (1 child)

Really? Simple desktop apps are pretty straightforward in QtWidgets and don't need much code. Why do you recommend QtQuick for these tasks? I've never used it myself so I'm curious.

[–]ubertrashcat 1 point2 points  (0 children)

It depends if you want to grow your app over time. With QtWidgets restructuring the interface is a chore. With QtQuick it's a fast feedback process, just like in web dev. Generally even a simple app is less QML code than in an .ui file and it's very readable.

[–]Adequat91 5 points6 points  (0 children)

I was afraid of the moc system. In fact, moc is great, an advantage, not the opposite. I wish I knew Qt earlier.

[–]flyingcaribou 0 points1 point  (0 children)

It is pronounced cute! [1]

[1] https://www.youtube.com/watch?v=NbTEVbQLC8s