I want to show only icons in my QListWidget. I set text to empty string. When I select an icon I see an empty selected square on the text place. See the screenshot:
How can I get rid of this empty space?!
use NULL instead
ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"),NULL));
How do you add an icon in your QListWidget? This should work fine (I am loading the icon from the resource file) :
ui->listWidget->addItem(new QListWidgetItem(QIcon(":/res/icon"), ""));
EDIT
From the screenshot I see that your problem is that there is some white space below the icon corresponding to the empty string. You could hack this behavior by setting a very small size to the font of the list widget item.
QListWidgetItem *newItem = new QListWidgetItem;
QFont f;
f.setPointSize(1); // It cannot be 0
newItem->setText("");
newItem->setIcon(QIcon(":/res/icon"));
newItem->setFont(f);
ui->listWidget->addItem(newItem);
This will do the trick. However you could also use the setItemWidget function and use your custom designed widget, or use a QListView and a delegate.
My solution was to call setSizeHint() on the item with the size of the icon. I added a little padding because the selection box was cut off without it.
QListWidgetItem * pItem = new QListWidgetItem(icon, "");
pItem->setSizeHint(iconSize + QSize(4,4));
listWidget->addItem(pItem);
An alternative solution where you do want to store text (as an identifier) but not show it, is not to set ANY text for the QListWidgetItems in the creator but instead store the text details in the data part.
Specifically you want to use this QListWidgetItem(QListWidget *parent = nullptr, int type = Type) constructor that can be used without any arguments. Then you can assign an icon, but no text afterwards, before inserting it into the QListWidget.
If you are putting the text in afterwards anyhow you'd be using QListWidgetItem::setText(const QString &text) and you just need to change that to QListWidgetItem::setData(int role, const QVariant &value) which in practice would be QListWidgetItem::setData(Qt::UserRole, const QString &text) and rely on a QString being convertible to a QVariant. Unfortunately there are some shortcomings going the other way:
To retrieve the value you need to explicitly convert the QVariant back to a QString - by using QListWidgetItem::data(Qt::UserRole)->toString()
To search for a particular match Qt does not provide a find means to identify the index(es) of the members of a QListWidget that match a particular role though it does for the original text - unlike, say a QComboBox which has findXXXX methods for both.
The project I code for had exactly this issue (we had unwanted text at the bottom which was set to a font size of 1 but it still showed up) which I eventually fixed like this and it works:
Related
I'm trying to achieve something like rich text editor in javafx, using RichTextFX library component: InlineCssTextArea.
I've expect an API similar to this:
InlineCssTextArea area = new InlineCssTextArea();
area.setStyle("-fx-font-weight: bold;");
//now user input is bold
area.setStyle("text-decoration: underline;");
//now user input is bold and underlined
area.setStyle("-fx-font-weight: normal;");
//now user input is just underlined
But it seems, that all style changes has to be applied to a range of characters. Did I miss something? This use case seems to be the most natural one.
Do I have to track current carret position, and apply changes accordingly?
I'm developing a RCP application, where I need to assign shortcut (Alt+[key]) to SWT Button with image.
I'm able to assign shortcut to Button with text using "&" character,
fox ex: button.setText("&Select All"); then Alt+S will act as shortcut fot that button.
I have 2 questions regarding this:
How to assign Alt+[key] shortcut to SWT button with images (no text), without using key listener?
How to assign Alt+[key] shortcut to SWT button with text, but no shortcut key letter in that text, again without using key listener.
for ex: "UnFix" is the text on button and shortcut key should be Alt+Q.
I hope there should be a way to do this in SWT.
You can select a mnemonic by placing an ampersand before the letter that should serve as the mnemonic letter (like "&Select All").
If the control does not have a text or the desired letter does not occur in that text, you will ned to use key event listener. There is no way around that.
Some applications work around the 'missing letter' in that they place the mnemonic letter in brackes like this: "UnFix (&Q)". Though this technically work, I find this an esthetically rather unfortunate choice.
Using an unobvious mnemonic letter has also usability issues: how would a user ever know or memorize that Alt+Q means 'UnFix'?
After trying lots of work arounds, I'm finally able to assign shortcut key to SWT button with image.
SWT Button.class make use of getText() method to find mnemonic, so overring getText() method of Button.class and returing mnemonic key will serve the purpose. checkSubclass() method should also be overridden to do nothing otherwise swt's Subclassing not allowed error will occure.
Button newButton = new Button(parent, SWT.PUSH){
#Override
public String getText() {
return "&N";
}
#Override
protected void checkSubclass() {
// Do Nothing to avoid Subclassing Not Allowed error.
}
};
newButton.setImage(newButtonImage);
I above example newButton has the shortcut key Alt+N.
I have a QGraphicsLinearLayout with a series of QGraphicsWidgets within. I can hide the widgets just fine, but the layout spaces out all of the remaining widgets as if the hidden ones are still visible. How can I get the layout to use this space?
My code is something like this:
//scene is a QGraphicsScene*, myWidget# inherits QGraphicsWidget
scene->addItem(myWidget1);
layout->addItem(myWidget1);
scene->addItem(myWidget2);
layout->addItem(myWidget2)
scene->addItem(myWidget3);
layout->addItem(myWidget3)
//then later, I call
myWidget2->hide();
But although myWidget2 is now invisible, the layout is still spaced as though it were there. How can I change that?
Thanks.
Try calling QGraphicsLinearLayout::invalidate() to clear any cached geometry information after hiding the widget. If that doesn't help I would assume that removing the widget from the layout (if that is feasible for you) should do it.
I think you are loking for QWidget::findChild<T>(Qstring name)
name - an object name which can be set with QObject::setObjectName(Qstring name)
T - is a type of an object you are loking for.
so in your case code should look like:
MyWidget* myWidget1 = new MyWidget(this);
myWidget1->setObjectName("myWidget1");
........
MyWidget* requiredWidget=scene->findChild<MyWidget*>("myWidget1");
I have a QLineEdit, on which I have set a QRegExpValidator, that allows the user to input only one whitespace between words.
Now I want that whenever the user tries to enter more than one whitespaces, the tooltip of the QLineEdit should show up, but I'm not getting any method to implement it.
Thanx :)
It seems there is no direct method to perform what you want. One way to do above is to handle QLineEdit's textChanged() signal. Then you can check that string against your regular expression using QRegExp::exactMatch() function and if it don't match then show tooltip.
Connect the signal..
...
connect(ui->lineEdit,SIGNAL(textChanged(QString)),this,SLOT(onTextChanged(QString)));
...
Here your slot goes..
void MainWindow::onTextChanged(QString text)
{
QRegExp regExp;
regExp.setPattern("[^0-9]*"); // For example I have taken simpler regex..
if(regExp.exactMatch(text))
{
m_correctText = text; // Correct text so far..
QToolTip::hideText();
}
else
{
QPoint point = QPoint(geometry().left() + ui->lineEdit->geometry().left(),
geometry().top() + ui->lineEdit->geometry().bottom());
ui->lineEdit->setText(m_correctText); // Reset previous text..
QToolTip::showText(point,"Cannot enter number..");
}
}
I don't remember explicit API for showing a tooltip. I'm afraid you'll have to go with popping up a custom tool window (i.e. a parent-less QWidget) to achieve the desired result.
If you want to style your own popup window like a standard tooltip, QStyle should have something for that. If in doubt, read into the Qt source code where it renders the tooltip. That'll tell you which style elements to use.
i want to display a second, not bold, text in my MessageBox, like seen for OSX here: http://welcome.solutions.brother.com/NR/rdonlyres/1EA4CC0C-F0B9-45D3-BD2C-EF2C430E3FAD/15107/error2.gif
Is there a way to do this with MessageBox? If not, I would create my own Dialog, problem is that i don't know how to load the appropriate icons.
Unfortunately, MessageBox is fairly constrained in its functionality. You can get the system icon from the Display class and then set it in a label:
final Image warningImage = getShell().getDisplay().getSystemImage(SWT.ICON_WARNING);
final Label imageLabel = new Label(dialogArea, SWT.NONE);
imageLabel.setImage(image);