Qt: using one widget in several layouts - qt

I have a QTabBar, and all tabs are supposed to have the same widget in them:
layout1->addWidget(w);
layout2->addWidget(w);
However calling addWidget the second time causes this widget to disappear in the first layout.
Is there any way to use one widget to insert it in several tabs?
Of course I can always create a new widget instance for every tab, but that takes extra time and memory.

Is there any way to use one widget to insert it in several tabs?
No. If you want the widget to be seen twice, then you need two instances of it.
Of course I can always create a new widget instance for every tab, but that takes extra time and memory.
Simple widgets are relatively cheap in regards to memory/time to create. Unless your GUI is becoming unresponsive and your profiler says that this is a problem, you are likely attempting a premature optimization.
Depending on the specific goal you're trying to accomplish, there are patterns such as using multiple views that share a single model that can potentially be employed.

Related

How to merge two QGraphicsItems into a custom one?

I have a problem that I don't know how to face and I was looking for some help. I recently started using Qt and I do not really know how Qgraphicsitems works.
I have a scene and I have to create a group of graphics items everytime my program starts, but sometimes I have to update some of those items that are always together.
So I was thinking about creating one Qgraphicsitem with all of them in it. That way i can handle them easily. My problem is that I'm not sure if that is a good approach. In a modular point of view it make sense because that group of items are always toguether and that way I do not have to create all of them everytime, but I do not know how to do it in the first plane how can I create a QgraphicsItem that has some QgraphicsItems in it?
Sounds like you're looking for QGraphicsItemGroup:
The QGraphicsItemGroup class provides a container that treats a group of items as a single item.
https://doc.qt.io/qt-5/qgraphicsitemgroup.html

Use Image in various dynamically loaded Components in QML

What I want to Achive:
Toggling the appearance of an Item, where the components that describe the appearance are loaded dynamically, but common elements are not loaded multiple times.
Lets say, I have a list of elements with an icon (e.g. cover) and a title (e.g. music title).
When clicked/dragged, it shifts the shape, so additional information can be displayed (e.g. duration, artist, ...).
Finally I have a DropArea, in which I can drop those elements. Here I only want to display the icon.
As I am informed correctly, it is not advisable to have all three forms pre-loaded and only shift the visibility and other parameters of the additional objects, as the first list is a ListView.
Therefore I decided, to create multiple components, and then load them with a Loader.
On the other hand, this leads to some overhead, for I load and unload the Icon, that is common to all the components, each time, the shape shifts.
My solution so far is, to load the image outside of the components, re-parenting it each time, the shape shifts. This however feels odd, and I am not sure, if this might not be the less performing way, compared to the "loading everything at once and resetting visiblity and positioners"-approach
What is the proper way to do this?
For the efficiency aspect, the most performing one should be that only dynamically loads something that is really dynamic, and leaves other things static(or so called declarative). In your app, the icon should be declarative, I think.
Regarding with reparenting, actually re-parenting is quite common in Qt Quick programming, especially within dynamically loading context. The parent in Qt Quick is not the same concept of QObject. It's just visual management (not memory management). That's why you can see Qt Quick even provides ParentChange in State/Transition.

Speed up creation of hundreds of buttons at runtime

I'm creating hundred of buttons at runtime and adding them into layout. Each operation probably trigger many things (repaint, layout rebuild etc) which are not necessary for each button but could be called once add the end when all buttons are created. In Lazarus world (open-source Delphi alternative) is DisableAutoSizing / EnableAutoSizing for form which "hold on" time-consuming triggers. Can't find anything similar for Qt.
I need also such functionality in another case. Let's say that you have complicated UI with a lot of widgets (with childs etc) and you want to totally rearrange UI (change layout, move widgets into another layouts etc). Normally I sometimes see "flickering". I would like to stop application painting - rearrange widgets - enable painting
Edit: Found something setUpdatesEnabled() . Is there anything else?

Creating a table dynamically, with multiple lines of text in each cell and each cell being clickable, in ASP.NET

I'm trying to design a calendar-type table that is built dynamically based on data from a separate database source.
This is an illustration of the basic design idea:
Here's an image:
If an object was added that was referenced to a Saturday, another column would be added and if the one on Monday was removed, the column would disappear and vice versa.
The weekdays are column headers and the squares represents cells with some lines of information. Each cell in its whole should be clickable.
The entire table should show a single month only.
I'm thinking this might be very time consuming to implement, and will quite quickly become impossible to re-read and understand later on if it isn't designed right. So if anyone has a more simple way to implement a month-view of a calendar, with some lines of information stored in each cell, and a possibility to call Server.Transfer() on the click event of a cell, it would be just as ideal.
As far as I know, the Calendar component doesn't support editing the contents of the cells in calendar, and other than that I don't really know which way would be the best way to go.
If I can improve the question in any way, please let me know.
I think what you want is not a Calendar, but rather a Scheduler control, which includes functionality to display a calendar view, add/edit "appointments" and provide events of what happens when you click them.
This thread lists a lot of such Scheduler control that you can use, some of them free.
Here are some additional links that implement similar functionality:
http://www.rekenwonder.com/aspnet/schedule.html
http://www.codeproject.com/Articles/7619/Databound-Schedule-Controls
http://www.codeproject.com/Articles/31766/DayPilot-Scheduler-Control-for-ASP-NET
I think reusing one of those components and spending some time to research them would be much less time consuming that implementing your own solution from scratch. If what you want falls into those common UI patterns supported by those controls, you'll be fine.
If, on the other hand what you want is not a common practice, you may rethink your approach.

Sharing a VBO across multiple QWidgets in JOGL

I'm trying to share create four QGLWidgets with the same GL3 context so I can share a VBO between them. I've been doing this for a while with just one widget, but it wasn't shared with the others. QGLWidget has a sharewith paremeter, which from what I understand automatically shares the contexts between them, but I'm not sure how compatible that is with JOGL.
I'm also confused about when the context is actually created. In some examples it says to create the context in initializeGL. I'm not sure if that means I have to update the first widget before I can create the secondary widgets (passing the sharewith paremeter the first created widget with a current context).
Can anyone provide me with a simple example to get this functioning? I just need to create four context-sharing GLWidgets that all run off a GL3 profile.
Although I'm not using JOGL, I am doing a similar thing here and here. The basic idea is that you create a hidden QGLWidget, make it current and compile all your shaders, then pass it as the shareWidget to your child viewports. Whenever you want to upload geometry
make the hidden QGLWidget current and do your glBufferData calls - the data becomes available to the other viewport contexts.

Resources