How can we know the width and height of string? - qt

I want to create a button exactly the same size as the string for this i want the width and height of the string.

To manually get the size of a string, you need to use the QFontMetrics class. This can be manually used like this:
QFont font("times", 24);
QFontMetrics fm(font);
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
If you want to calculate it for the font used in a given widget (which you may not know), then instead of constructing the fontmetrics, get it from the widget:
QFontMetrics fm(button->fontMetrics());
int pixelsWide = fm.width("What's the width of this text?");
int pixelsHigh = fm.height();
Then you can resize the widget to exactly this value.

Use QFontMetrics.
Example: http://www.developer.nokia.com/Community/Wiki/CS001349_-_Calculating_text_width_in_Qt

Related

Fit QDialog window to size of text

I have a QDialog class that takes in a QString. I am calling setFixedSize with a set width and height but I want the QDialog to be more dynamic and fit to the size of the text.
I have tried adjustSize() but all that did was shrink the window to the point where the text was cut off.
ConfirmDialog::ConfirmDialog(const QString& message, QWidget* parent)
: QDialog(parent)
{
setFixedSize(WIDTH, HEIGHT);
statusLabel->setText(tr("Confirmation"));
statusDetailsLabel->setText(message);
statusDetailsLabel->setWordWrap(true);
}
I always see a Window with size of dimensions WIDTH and HEIGHT. I want it to fit the test.
One way would be to use Font Metrics to get the bounding rects of each label, and then set the window size to the sum of both rects + some padding to make it look nice.
One problem you will run into is having wordwrap on. How do you determine the width of the window, if you are word wrapping? So I've added a "MAXWIDTH" for the window. If your text is shorter and does not require word wrap - the window will shrink to fit it. If it does require word wrap, it will not go over your set size.
ConfirmDialog::ConfirmDialog(const QString& message, QWidget* parent)
: QDialog(parent)
{
const int MAXWIDTH = 400;
const int VERTICALPADDING = 50;
// Create Layout
QLabel *statusLabel = new QLabel(this);
QLabel *statusDetailsLabel = new QLabel(this);
QVBoxLayout *layout = new QVBoxLayout();
layout->addWidget(statusLabel);
layout->addWidget(statusDetailsLabel);
setLayout(layout);
// Populate Text
statusLabel->setText(tr("Confirmation"));
statusDetailsLabel->setText(message);
statusDetailsLabel->setWordWrap(false); // Start w/ word wrap off.
// Font metrics to get the sizes of our text.
QFontMetrics fontMetricsLabel(statusLabel->font());
QFontMetrics fontMetricsDetail(statusDetailsLabel->font());
// Get max width - label or detail lable, whichever is longer.
int width = std::max(fontMetricsLabel.boundingRect("Confirmation").width(),
fontMetricsDetail.boundingRect(message).width());
// Check that we do not go over our MAXWIDTH.
if(width > MAXWIDTH) width = MAXWIDTH;
// Enable word wrapping.
statusDetailsLabel->setWordWrap(true);
// Get the heigts of both boxes.
int height = std::max(fontMetricsLabel.boundingRect("Confirmation").height(),
fontMetricsDetail.boundingRect(message).height());
// Set window size.
this->setFixedSize(width, height + VERTICALPADDING);
}

QT - Increase Richtext size on Button click

How do i increase the size of a Rich Text on the click of a button ?
I have a QTextEdit box with Rich text pasted in it.On the click of a + [ui button] i need to increase the font size of all the text inside it. Any idea on how to do that ?
Solution
This is what you should do inside the slot :
//-------------------------desired format-------------------------------
qreal pointSize = 40; // 40 for example, you can parameterize it
QTextCharFormat format;
format.setFontPointSize(pointSize);
//----------------------------------------------------------------------
ui->textEdit->selectAll();
// ^^^^^^^^^^^ You ask for all text in the textedit
// But remember partially change with mouse selection is also doable
ui->textEdit->mergeCurrentCharFormat(format);
(P.S. ui->textEdit is a pointer to QTextEdit)
The key point is to create an instance of QTextCharFormat to set the "partial" information of the font (Ex: size information only) and use QTextEdit::mergeCurrentCharFormat to merge the original format with the new format.
For example:
After merging by the operations above, the color, font...etc except size will be retained:
You can use the QTestEdit::setCurrentFont() function. For example:
QTextEdit te;
QFont f = te.currentFont();
int oldPointSize = f.pointSize();
int newPointSize = oldPointSize + 10;
f.setPointSize(newPointSize);
te.setCurrentFont(f);
te.setText("Test");
te.show();

Why the QFontMetrics::boundingRect() return a wrong size rect?

I'm using Qt4.7.
When I use QFontMetrics to render my text in some situation, I got a wrong width. My code is like this:
QFontMetrics fm(QApplication::font());
QRect rc = fm.boundingRect(str);
I found that fm.boundingRect(str) always return a fixed rect while the dpi changed.

GtkToolButton with custom icon but of stock icon size

I've a GtkToolBar which has say 3 GtkToolButtons with each of these having a stock icon value, and hence they all appear in the same size; now I added a 4th GtkToolButton with a custom image (.png), which was of an arbitrary dimension and only this button ended up looking huge (since the image was of higher resolution). What do I do to scale this GtkToolButton to match the other 3 buttons?
Here's the code which does what I briefed about:
GtkWidget *custom_icon = gtk_image_new_from_file(path);
GtkToolItem *toolbar_item = gtk_toggle_tool_button_new();
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(toolbar_item), custom_icon);
gtk_tool_button_set_label(GTK_TOOL_BUTTON(toolbar_item), "Custom Item");
gtk_toolbar_insert(toolbar, toolbar_item, -1);
Here is another solution.
GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(icon_file_path, NULL);
int width, height;
gdk_pixbuf_get_file_info (icon_file_path, &width, &height);
gtk_icon_theme_add_builtin_icon ("custom_icon", width, pixbuf);
g_object_unref (G_OBJECT (pixbuf));
GtkToolItem *toolbar_item = gtk_toggle_tool_button_new();
gtk_tool_button_set_icon_name (GTK_TOOL_BUTTON(toolbar_item), "custom_icon");
If you have the image in different sizes, you can add them all and let Gtk choose the one of the correct size (or resize if not found): Just repeat the first five lines for each of the image files.
You can use your icon anywhere else and its size will also be adjusted automatically.
For example, to use it for your main window:
gtk_window_set_icon_name(GTK_WINDOW(main_window), "custom_icon");
Found it out myself! Here's the trick so that it helps someone like me. Query the icon size from the stock menu item, which is a enum (standard values like GTK_ICON_SIZE_BUTTON, GTK_ICON_SIZE_LARGE_TOOLBAR, etc.). Now get the pixel size using gtk_icon_size_lookup. Create a pixbuf from the custom icon/image file with the right dimensions. Create a GtkImage from that and set it to the new menu item and you're done!
GtkToolItem *stock_menu_item = gtk_toggle_tool_button_new_from_stock(GTK_STOCK_NEW);
GtkIconSize toolbar_icon_size = gtk_tool_item_get_icon_size(stock_menu_item);
gint width = 0, height = 0;
gtk_icon_size_lookup(toolbar_icon_size, &width, &height);
GdkPixbuf *app_icon = gdk_pixbuf_new_from_file_at_size(icon_file_path, width, height, NULL);
GtkImage *tray_icon = gtk_image_new_from_pixbuf(app_icon);
g_object_unref(app_icon);
app_icon = NULL;
GtkToolItem *toolbar_item = gtk_toggle_tool_button_new();
gtk_tool_button_set_icon_widget(GTK_TOOL_BUTTON(toolbar_item), tray_icon);

How to set minimum height of QListWidgetItem?

How can I set the minimum height of a QListWidgetItem? I'm using QListWidget::setItemWidget() with a customized widget, and although I explicitly declared minimum height of my customized widget, those QListWidgetItems still have a pretty low height attribute.
To set minimum height of each individual QListWidgetItem you can use sizeHint() function. For example, following code will set minimum height of all the QListWidgetItem to 30px..
int count = ui->listWidget->count();
for(int i = 0; i < count; i++)
{
QListWidgetItem *item = ui->listWidget->item(i);
item->setSizeHint(QSize(item->sizeHint().width(), 30));
}
Hope this helps..
Use setSizeHint on the items.
void QListWidgetItem::setSizeHint ( const QSize & size )
This is the right method for telling the delegate how much screen it must preserve for the item.
Look at http://qt-project.org/doc/qt-4.8/qlistwidgetitem.html#setSizeHint

Resources