I know how to italicize the entire text of a QTreeWidgetItem:
QTreeWidgetItem* elt = new QTreeWidgetItem(item);
QFont font = elt->font(0);
font.setItalic(true);
elt->setText(0, choice);
elt->setFont(0, font);
But is it possible to italicize only part of that text? (e.g. the first one or two words)
It's possible with:
QTreeWidgetItem *item = new QTreeWidgetItem(treeWidget);
QLabel *label = new QLabel("<i>italics</i>, <b>bold</b>, normal", treeWidget);
treeWidget->setItemWidget(item, 0, label);
but cleaner solution could be by using QTreeView and subclassing QItemDelegate.
There is no such option by default. You need to set a QItemDelegate that is able to render HTML. See this solution.
Related
I'm trying to create an exact copy of the following table in Qt
How would I create a header like that? Is there a way to do it in Qt?
You could customize every single header column by using setHorizontalHeaderItem or just set the text in all column header by using setHorizontalHeadersLabels.
An easy way to implement your attached image is by customizing your own QWidget. Use a vertical layour and consider your Icp (mA) header as a custom label with a center alignment.
Then insert your QTableWidget and set the headers as (3kOmega, 5.1kOmega & 11kOmega). Something like this:
QWidget* container = new QWidget(this);
QVBoxLayout* layout = new QVBoxLayout(this);
// Custom label
QLabel* header = new QLabel(this);
header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
header->setAlignment(Qt::AlignHCenter);
header->setText("Icp (mA)");
// Custom QTableWidget
QTableWidget* table = new QTableWidget(this);
table->setColumnCount(3);
QStringList LIST;
LIST << "3k" << "11k" << "15k";
table->setHorizontalHeaderLabels(LIST);
table->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
table->horizontalHeader()->setStretchLastSection(true);
layout->addWidget(header);
layout->addWidget(table);
container->setLayout(layout);
setCentralWidget(container);
Giving you something like this:
I would like to add two widgets (say QPushButton) to the status bar, one to the left and other to the right side.
I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.
PS: I tried using addWidget() to add to the left and addPermanentWidget() to add to the right but it doesn't look neat and also it doesn't feel right.
You can add two buttons to a layout in a widget and add the widget to the status bar using QStatusBar::addWidget :
QWidget * widget = new QWidget();
QPushButton * leftBut = new QPushButton("Left");
QPushButton * rightBut = new QPushButton("Right");
QGridLayout * layout = new QGridLayout(widget);
layout->addWidget(leftBut,0,0,1,1,Qt::AlignVCenter | Qt::AlignLeft);
layout->addWidget(rightBut,0,1,1,1,Qt::AlignVCenter | Qt::AlignRight);
ui->statusBar->addWidget(widget,1);
I am thinking of adding horizontal spacer in between the two widgets, but don't know how to add.
Here is a way to use a "fake" spacer.
QPushButton *leftButton = new QPushButton("Left");
QPushButton *rightButton = new QPushButton("Right");
QLabel *spacer = new QLabel(); // fake spacer
ui->statusBar->addPermanentWidget(leftButton);
ui->statusBar->addPermanentWidget(spacer, 1);
ui->statusBar->addPermanentWidget(rightButton);
The second parameter in addPermanentWidget is "used to compute a suitable size for the given widget as the status bar grows and shrinks".
Demo:
I think the simplest way is using a QGridLayout (honestly I never tried to modify a status bar anyway) supposing that the status bar is or descends from widget you can do this:
QGridLayout *myGridLayout = new QGridLayout();
statusbar->setLayout(myGridLayout)
QPushButton *button1 = new QPushButton(this);
myGridLayout->addWidget(button1,0,0,1,1);
QPushButton *button2 = new QPushButton(this);
myGridLayout->addWidget(button2,X,0,1,1);
The biggest is X the more space you want to leave in between, I would suggest to start with 3 and then make few tests to see how it looks.
I've got a dynamically created list of image extensions that I want to display checkboxes for.
I have a QStringList that contains .png, .jpg, .bmp. This list can be altered by the user so I need to generate a checkbox on the UI for each possibility.
QStringList filters;
filters << "*.jpg" << "*.png" << "*.jpeg";
I was thinking about a foreach or for loop to achieve this.
foreach(QString filt, filters){
QCheckBox *checkbox = new QCheckBox(filt, this);
}
This puts 3 checkboxes on the UI but they are all on top of one another.
How can I space them and also, how can I work with the change in state from check to unchecked on the fly?
Absolutely lost about how to do this when it's generating checkboxes from a stringlist.
Thanks.
The checkboxes are stacking on top of one another because you are not adding them to the widget's layout. Here's an example that will put each checkbox into a vertical layout.
QWidget *w = new QWidget;
QVBoxLayout *vbox = new QVBoxLayout;
foreach(QString filt, filters){
QCheckBox *checkbox = new QCheckBox(filt, this);
checkbox->setChecked(true);
vbox->addWidget(checkbox);
}
w->setLayout(vbox);
w->show()
Read up on QVBoxLayout at http://qt-project.org/doc/qt-5.0/qtwidgets/qvboxlayout.html
I use QTreeView and QStandardItemModel to display its contents (read from xml file) in a tree view. The file parser works just OK, but when I use the view to display the data, the sizePolicy just doesn't work.
Why? How to make the area of tree expand?
(It shows like below, which tree view doesn't expand?)
picture of the display of the tree:
code (edited to add "layout" part, but not work, either.):
...
QStandardItemModel *model = new QStandardItemModel();
preOrder(doc.firstChild(), model);
view = new QTreeView(pageDetails);
view->setModel(model);
model->setHorizontalHeaderLabels(QStringList(""));
QHBoxLayout* lay=new QHBoxLayout(pageDetails);
lay->setContentsMargins(0, 0, 0, 0);
lay->addWidget(view);
view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
pageDetails->setLayout(lay);
view->show();
I got the problem solved now:
But still don't know how to remove the header...
The problem is caused by Qt Designer. I use it to add the parent widget ui, but set it a horizonal layout in the designer(somewhat difficult to find).
I just don't know why the code above just doesn't work in this case.
PS:
If I left the code commented:
//QHBoxLayout* lay=new QHBoxLayout(pageDetails);
//lay->setContentsMargins(0, 0, 0, 0);
//lay->addWidget(view);
//view->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
//pageDetails->setLayout(lay);
It appears:
PS2:
remove header successfully!:
comment model->setHorizontalHeaderLabels(QStringList(""));
add view->header()->setVisible(false);
I'm just a beginner and I wonder if it would be possible to create a form widget on Mac like the info dialog in iTunes.
I tried using:
QGroupBox: I cannot find a way to get rid of the frames.
Creating my own widget: I cannot find a way to fix the spacing between the label and the QLineEdit widget using the QVBoxLayout (actually I'm not sure I understand well the differences between margin/spacing).
QFormLayout: I cannot find a way to reduce the size of the QLabel after using setrowWrapPolicy::WrapAllRows
Also I am not (yet) very comfortable with QtDesigner, so i'd like to avoir using it (for now)
Thanks in advance
Edit: Some precisions on the programs. I use QtCreator 2.6.1 with Qt 4.8.1 and 5.0 on Mac OS X Mountain Lion.
Edit 2: Here is the code.
Subclass of QWidget:
MCLineEdit::MCLineEdit(const QString &header)
{
m_lineEdit = new QLineEdit;
m_lineTitle = new QLabel(header);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(m_lineTitle);
layout->addWidget(m_lineEdit);
layout->setSpacing(0);
setLayout(layout);
}
To display the widget
myView::myView(QWidget *parent) :
QWidget(parent)
{
setFixedSize(600, 500);
MCLineEdit *lineEdit1 = new MCLineEdit("Test 1");
MCLineEdit *lineEdit2 = new MCLineEdit("Test 2");
MCLineEdit *lineEdit3 = new MCLineEdit("Test 3");
MCLineEdit *lineEdit4 = new MCLineEdit("Test 4");
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(lineEdit1);
mainLayout->addWidget(lineEdit2);
mainLayout->addWidget(lineEdit3);
mainLayout->addWidget(lineEdit4);
mainLayout->setSpacing(0);
setLayout(mainLayout);
}
This can be accomplished a ton of ways I am sure. Qt gives you all the possible layouts you could need to achieve this. You could do it with a QGridLayout, and add widgets with different amounts of "cell" spanning, and control the row and column sizes to suit. Or you can just do it with a bunch of nested vertical/horizontal layouts.
For example, you can group a label and a field together in a QVBoxLayout, by adding the widgets with a left alignment, and then setting the spacing to 0 between the items:
layout->setSpacing(0);
layout->addWidget(aLabel, Qt::AlignLeft);
layout->addWidget(aLineEdit, Qt::AlignLeft);
mainVerticalLayout->addLayout(layout);
For something like the track numbers, it is just more nested layouts:
vLayout->addWidget(aLabel);
hLayout->addWidget(aCheckbox);
hLayout->addWidget(aLabel);
hLayout->addWidget(aCheckbox);
vLayout.addLayout(hLayout);
And regarding your bullet points:
QGroupBox lets you remove the frame with setFlat(bool)
With layouts, the margin is the padding around the outside of the contained widgets. What you want is setSpacing(int) to control the amount of space between the items in the layout.
QFormLayout is probably not your best choice here. That is usually for having labels on one side and widgets on the other. Basically it is a 2 column layout. A QGridLayout would be more appropriate. And to reduce the size of the QLabel, you can give it a max or a fix size. Such as using setFixedWidth() or setMaximumWidth() on the label.
So I finally managed to get the expected result after playing a bit with QtCreator.
Here is the code for who might be interested:
myLineEdit:myLineEdit(const QString &header)
{
m_lineEdit = new QLineEdit;
m_groupBox = new QGroupBox;
QFont groupFont;
groupFont.setPixelSize(10);
groupFont.setBold(true);
m_groupBox->setTitle(header);
m_groupBox->setFlat(true);
m_groupBox->setFont(groupFont);
m_groupBox->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
QFont lineFont;
lineFont.setPixelSize(13);
lineFont.setBold(false);
m_lineEdit->setFont(lineFont);
QHBoxLayout *layout = new QHBoxLayout;
layout->addWidget(m_lineEdit);
layout->setContentsMargins(0, 10, 0, 0);
layout->setSpacing(10);
layout->setSizeConstraint(QLayout::SetMinAndMaxSize);
m_groupBox->setLayout(layout);
QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(m_groupBox);
setLayout(mainLayout);
}
One comment though: will only work on Qt 5 since on 4.8 the setFlat() method will display a separating line between the header and the QLineEdit.
Thanks to jdi for his help!