is there a numeric Qt edit box with more than one spinner? - qt

Before i embark on developing a custom control i wonder if somebody would know of a QT control consisting of an edit box (for a number) with more than one spinner next to it. One spinner would be used to increment/decrement the number by 10's, the other by 1's. Ideally, one could allow even three spinners (for the 100's).
There is no standalone spinner but i'm thinking maybe putting several small scrollbar controls next to each other would do.

I highly doubt that there is such a control.
Note however that a default QSpinBox supports incrementing/decrementing in steps of 10 by pressing the Page Up or Page Down keys on your keyboard.

Thanks for the tip!
I ended up creating a horizontal layout with 0 spacing, then placing three QDoubleSpinBox instances in it. On the second and third i set the max width to 15 to just show the spinners. Visually it looked great: an edit box with three spinners.
I then connected the value changed signal from the first to the second and third and the value changed from the second and third to the first. So then clicking on any of the spinners changed the value on the first correctly. Finally, i adjusted the stepsize on the spinners as needed.
It was a lot easier than what i thought it would be.

Related

JavaFX combo box start value?

I was curious if there is a way to set a start value for a combo box so that it does not start at the topmost value. For example, I have a combo box called cbCurrentWeight with values ranging from 10 all the way to 999. I don't want 99% of users to have to scroll 100-200 numbers so they can select their weight. I would like for the combo box to start at 100 with the option to go higher or lower from there.
I do not want to set a default value for this combo box either.
Would I be better off just using a TextField and adding parameters to ensure users are entering a valid number?
If you'd like to go the simple route, a TextField with input validation is probably best.
Another thing you could do is create your own version. Where you display the number 100 with a (+/-) button next to it and every time a user clicks the button you increment/decrement the value. You can even add a TextField for step size so users can increment by 20lbs or 5lbs at a time.
I decided to simply change the value to a minimum of 50lbs. The spinner option was great though, and I got it to work. I just prefer the view of the ComboBox.
This is absolutely doable, here is the code:
combo.getSelectionModel().select(90);
This would have the combo box set itself to the 90th option. Assuming you have values 10-999 in order, this would make your combo box set itself to the 100 option.
When you call
combo.getSelectionModel().
Here are the different ways you can modify a combo boxes selection: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/SingleSelectionModel.html

How to do a context dependant layout in Qt Designer and PyQt5?

I have a group box or the like containing a combobox and some more widgets that shall not all be visible at the same time. Depending on the selection I want at the same place to be displayed a button or a line edit or a (second) combobox. So if combobox entry one is selected it shows the button, if entry two is selected it shows the line edit, if entry three is selected it shows the second combobox, while not showing the other two.
I know that I can do it all dynamically but this results in a lot of code lines. I want to use QT Designer and a grid layout and I can't see how to put two widgets at the same place and then make only one visible programmatically.
I hope I could explain properly.
I played with Qt Designer but could not place two widgets over one another in a grid layout.
Thanks for any help.
Thank you G.M.
Yes, I was looking for QStackedWidget. I wasn't aware of this at all. I tried it and it works easily out of the box.

'Manual' multi-selection in QTableView and partially hidden grid

I know that topic about multi-select arise at least once, but honestly neither can't find it anymore, nor remember that it had and decent solution.
There are two questions I propose to discuss:
1) Creating a behaviour similar to MS Excel cell's selection. So user click one cell in QTableView and gets clicked cell highlighted and in additional several 'dependant' cells change appearance (get selected or just gets highlighted in any way). In Excel it's widely used to show cell formula dependencies.
I know there are several approaches to solve it. Most simple one is to modify view selection with dependant cell in any of appropriate signal handlers (for example QAbastractModelView clicked()). That way does the job but has ugly side effect, that due to fact that signals delivered after redraw of selected cell occurs so dependant selection is drawn after first cell which produce flickering.
Second approach is go Delegate way.. That's also have some issues because you get paintEvent only for selected cell, so there is not that much you can do about 'dependent' cells. Actually I was able to solve it through this way, by catching on-click, modifying selection and using completely custom delegate which draws everything as soon as complete selection is formed, so actually it skips first redraw, but again I wasn't completely satisfy with results although visually it looked completely right.. mostly because overall TableView response time decreased a lot. Reasons for that is Qt draws native selection right after mouse click received before sending any signals to user classes and in case of this approach paintEvent in delegate arrives after several main loops. So there is a noticeable delay in case of using 'draw selection in delegate' in compare to 'draw native selection'.
I am already starting to think that best option can be completely overwrite most of QTableView to add support for such selection schemas, but may be there are more straight approach?
2) Second question (I put them together because there are something common issues).
Let say you have a grid representing financial information by months and within a month there are several columns of information, so block of N columns repeated M times. Obvious way to make such grid more readable is to use different style for vertical lines in grid for first data column in each month. Let's say to make them 1-2 pixels wider.
As you cannot specify grid style per cell, what I did was to setGrid(false) and then draw my own grid lines as a cell content in delegate.
But then I faced a problems from point 1. Then you instruct Qt to use delegate on certain cell, before delegate will get a paintEvent Qt clears a background of the cell. And in case of hidden grid the background rect which Qt clears is one pixel bigger then required. Probably it can be consider to be a Qt bug because they dont respect grid visibility, but this results in removing grid lines in neighbour cell, so you have to draw in delegate not only cell own grid, but also recalculate proper cell rectangle, check if Qt made a mistake (by analysing QPainter rect), decide if whats being removed from neighbour cell needs restore and repaint it also. This leads to really complicated delegate logic and I cannot consider it to be a decent solution.
So question 2 can be rephrased as do we know a decent way to style a grid per cell in QTableView?
OMG,so many words,can you just pick the most important info?
I'd do something like that:
Create a delegate. Subclass QAbstractTableModel and reimplement data method. Your implementation should return cell text for Qt::DisplayRole, but also can return whatever you want if role is one of your user-defined roles (like font or color or whatever of cell's text. You can use any role number above Qt::UserRole). Your model should emit dataChanged signal to notify QTableView that the content is changed and should be redrawn.
Then in delegate you just request this data using your overloaded QAbstractTableModel::data and draw it the way you want.

How can I create a draganddrop wrapper inside a table row, that can represent the whole row

When I use treetable in Vaadin, it is working fine drag and drop the whole row when click the left and right blank ereas. but that is not user friendly, I wanna create a draggable layout inside the row, let's say the 1st component of the each row, that when clicking it, the whole row is selected and can be draged and dropped to reorder.
The thing is I can create a vertical layout with wrapper doing that, but drag mode is only for its component or the wrapper itself, and when doing the drag action, it doesn't actually showing the whole row is moving, which may confuse the clients. What can I do to make it looking like I am dragging the whole row, similar with the one you drag the blank area of each row? Thanks.
I'm afraid there currently is no way for adding a wrapper that would extend the entire row instead of just one cell. To do this you'd need to extend the client-side implementation of Table (VScrollTable) and this is not something I would recommend as it is quite complex.
I do think that what you describe should be possible in core Vaadin (without using drag & drop wrappers), so could you please file a ticket at dev.vaadin.com? And please attach a small application showing the problem to make it easier for us to see the problem and fix it.
Anyways, in order for you to get it working today I'd suggest that you change your UI design a bit and for example borrow drag handles from iOS. By this I mean that you could add an icon that suggests "draggable" as a background-image in the first cell (or in the row header cell of each row) that encourages the user to grab the row there, where it is "empty" and dragging works. This way the user might not be too confused if dragging only works reliably in some parts of the rows.

How to show only 30 rows and hide the remaining rows of QTableWidget

I'm having a QTableWidget with 10,000 records. I need to show only 30 rows at a time and hide the remaining rows. While dragining/clicking vertical scrollbar it should show corresponding rows and hide the other rows.
ie, if one clicks on upper scroll button it should show one more upper item and hide one lower item and vice versa. It should happen while scrolling as well.
Can anyone help me in doing this?
I think you can trap the events of up/down buttons as Nicholas said, for the tracking of slider/scroll bar u need use 'setTracking(true)'followed by the handling of signal either 'valueChanged(int)' or 'sliderMoved(int)'.
Thanks,
Pradeep
Qt unfortunately won't make this easy for you, there is ways and means of doing it however. Depending on how you're populating your widget I'd recommend setting it up so you initially populate your 30 rows, you then need to trap the signals coming from the scroll buttons being clicked and tell the table widget to remove the upper/lower item and add the next one, there's trade off's to this method, but it'll make it easier than trying to maintain a large list of hidden rows.
I don't know if explicitly showing/hiding things is the way you want to go. Instead, take a look at Qt's model and view classes. If you are using a database, have a look at QSqlTableModel, which should handle these things for you. Otherwise, have a look at the Model-View programming document, their related examples, and especially the portion about optimizing for performance (although in a model, 10,000 records isn't really all that many for most uses).
There is a way to change table scrolling from pixels to items. In Designer Property Editor for the table, in the QAbstractItemView section, there is a VerticalScrollMode selection. This can either be ScrollPerPixel or what you probably want, ScrollPerItem.
That may change the behavior of the signal from pixels to items which would make your calculations easier.
I'm using 4.4.3.

Resources