How to resize QListWidgetItem according to it's content? - qt

I'm adding a QListWidgetItems to a QListWidget. Is there any way to set the size of the QListWidgetItem according to it's content data?
QSize size(50, 20);
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
newItem1->setSizeHint(size);
listWidget->addItem(newItem1); //listWidget is previously created
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
newItem2->setSizeHint(size);
listWidget->addItem(newItem2);
Text of newItem1 is displayed without any problem. But the newItem2 text is not fully displayed. It only shows few characters and then "..." as text elide. How to show the complete text without the elide? I want to set the size according to the size of item's data without setting any constant numbers.

I simply copy/pasted your code, removed setSizeHint() function call for both items and added some of mine. Here's working code:
QListWidgetItem* newItem1 = new QListWidgetItem();
newItem1->setText("short text");
ui->listWidget->addItem(newItem1);
QListWidgetItem* newItem2 = new QListWidgetItem();
newItem2->setText("this is a very long text");
ui->listWidget->addItem(newItem2);
ui->listWidget->setFixedSize(ui->listWidget->sizeHintForColumn(0) + ui->listWidget->frameWidth() * 2,
ui->listWidget->sizeHintForRow(0) * ui->listWidget->count() + 2 * ui->listWidget->frameWidth());
As you can see, both items are fully displayed.

Actually I have accidentally enabled uniformItemSizes for the QListWidget. When I disable that, QListWidgetItems were automatically resized according to it's content.

Related

Display big image in QListWidget

I want to use QListWidget to display big images, e.g. 3508×4961 size in pixels, each list item will display one image. The image is set in a Qlabel and the Qlabel is set into the QListWidgetItem by setItemWidget(). When my program is running, the list item cannot display the entire image because the image is too large. It only displays the upper part of an image. When I scroll down, the list item changes to the next image immediately instead of showing the lower part of the current image gradually. Does anyone know how to show the lower part of each image by scrolling?
Here is my code,
QImage *image = new QImage("/home/sk/image1.png");
QLabel *label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
QListWidgetItem *ite = new QListWidgetItem;
auto size = label->sizeHint();
ite->setSizeHint(label->sizeHint());
size = ite->sizeHint();
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
image = new QImage("/home/sk/image2.png");
label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
ite = new QListWidgetItem;
ite->setSizeHint(label->sizeHint());
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
image = new QImage("/home/sk/image3.png");
label = new QLabel;
label->setPixmap(QPixmap(QPixmap::fromImage(*image)));
ite = new QListWidgetItem;
ite->setSizeHint(label->sizeHint());
ui->listWidget->addItem(ite);
ui->listWidget->setItemWidget(ite, label);
have you tried changing the scroll mode to ScrollPerPixel
setVerticalScrollMode
and
setHorizontalScrollMode
ui->listWidget->setVerticalScrollMode(QListWidget::ScrollPerPixel);
and in order to change the scroll step
ui->listWidget->verticalScrollBar()->setSingleStep(10);
ui->listWidget->verticalScrollBar()->setPageStep(20);
these values will be used for scrolling

Bold Text Parts in ControlsFX Notification

I'm trying to write bold text inside the body of a Notification.
Notifications.create()
.title("My Title")
.text("This is <bold>text</bold>")
.showInformation();
As far as I can tell there's only the .text("my text") method to set the text of a Notification.
However I'd like to use a TextFlow in there to modify certain parts of a text.
I've also tried to use CSS for this but that only lets me change to overall appearances of the Notification as setting specific text through CSS is explicitly not supported.
My question now is: Is there a way to style parts of a text inside a Notification differently?
Got it working now.
The solution is to use .graphic(Node node). If you still want to display an Image at the left side you need to put everything in a Pane and add some padding.
I yoinked the original image from here.
BorderPane borderPane = new BorderPane();
borderPane.setLeft(new ImageView(Controller.class.getResource("/dialog-information.png").toExternalForm()));
TextFlow textFlow = new TextFlow();
textFlow.setPadding(new Insets(15, 0, 5, 5));
Text text1 = new Text("This is ");
Text text2 = new Text("bold");
text2.setStyle("-fx-font-weight: bold");
textFlow.getChildren().addAll(text1, text2);
borderPane.setRight(textFlow);
Notifications.create()
.title("My Title")
.graphic(borderPane)
.hideAfter(Duration.seconds(10))
.show();
It is important to use .show() and not .showInformation/Error/Warning() because that would override the BorderPane.
Result:

JavaFx TextFlow does not resize on GridPane on change of value

I have a TextFlow in a GridPane, when I change its value to a wider one it does not increase its width accordingly on display but truncates. I am a newbie at Java and I've probably missed something simple.
Initial value is built as follows;
TextFlow mhText = new TextFlow();
// Loop to build TextFlow
for(Card c : myGame.me.playerHands.get(0).handCards) {
Text text = new Text(c.cardText(false) + " ");
if(c.redSuit()) {text.setFill(Color.RED);}
mhText.getChildren().add(text);
}
playerGrid.add(mhText, 1, pgRow)
Then later it is rebuilt...
mhText.getChildren().clear();
for(Card c : myHand.handCards) {
Text text = new Text(c.cardText(false) + " ");
if(c.redSuit()) {text.setFill(Color.RED);}
mhText.getChildren().add(text);
}
Result screenshot
You can just see from the screenshot that only a dot from the additional 3rd 'card' is showing, I think due to truncation. I've tried using setPrefWidth and setMinWidth without success.
I can solve the problem by removing and re-adding the TextFlow on the GridPane,
playerGrid.getChildren().remove(mhText);
playerGrid.add(mhText, 1, myHandRow);
but I would be surprised if this is the right way, any advice is welcome.

How to add widgets to QToolbox item

I need to auto generate some UI forms in code to display message contents.
I want to use QToolbox, with an item for each message type. I then want to add labels and line edit to the contents of each tab, depending on the message protocol. I cannot seem to programaticaly add widget items to the toolbox item.
Below is my current code segment. The ui->frame is just a container for the toolbox. I will worry later about layout.
In my code, I create a frame and then some labels with parent set to the frame. Then I add the frame as an item to the toolbox.
QToolBox *qtbMainToolbox = new QToolBox(ui->frame);;
qtbMainToolbox->setGeometry(0,0,2000,900);
QFrame *frm1 = new QFrame;
QLabel *lbl1 = new QLabel(frm1);
QLabel *lbl2 = new QLabel(frm1);
QLabel *lbl3 = new QLabel(frm1);
QLabel *lbl4 = new QLabel(frm1);
iRetVal - qtbMainToolbox->addItem(frm1 ,"Test");
There is no visible element in your widgets; icon or any text.
You have to Set icon or Text to Your QLabel.
QToolBox *qtbMainToolbox = new QToolBox(ui->frame);;
qtbMainToolbox->setGeometry(0,0,2000,900);
QFrame *frm1 = new QFrame;
QLabel *lbl1 = new QLabel("Hello World",frm1);
iRetVal - qtbMainToolbox->addItem(frm1 ,"Test");
try above code.

Qt QTableView 1 line vertical size

I have the following to add a QTableView to a QWidget:
QVBoxLayout *vLayout = new QVBoxLayout(this);
QTableView *tableView = new QTableView;
tableView->horizontalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setStretchLastSection(true);
tableView->verticalHeader()->setVisible(false);
vLayout->addWidget(tableView);
This widget will use model which load data from MySQL... And there is only one line of content, so I would like to make the view just height enough to show one line. How to approach this issue?
I had the same problem and found that this works best:
const auto height = table.horizontalHeader()->sizeHint().height() + table.rowHeight(0);
table.setMinimumHeight(height);
table.setMaximumHeight(height);
You force the table to keep the exact size specified by you: the header size + the size of a single row.

Resources