QListWidgetItem with Radio Button - c++

I'm working on my first QT application and I have a problem with QListWidgetItems.
I will be having different kind of list.
for checkboxed list using:
listElement[i]->setFlags(Qt::ItemIsEnabled);
listElement[i]->setCheckState(Qt::Unchecked);
works exactly as wanted.
But now I want a Radio Button list. so my question is in two parts
can use the same logic that I used for checkBox to create Radio Buttons?
I have used:
listElement[i]->setFlags(Qt::ItemIsEnabled);
QRadioButton *radio1 = new QRadioButton(0);
dlList->setItemWidget(listElement[i],radio1);
this will display Items in the list with a radio Button, the problem is that the text is Over the Radio Button:
going to try to demonstrate without image
This is a test
o
for elements 1
instead for checkbox I have
This is a test
[]
for element 1
how can I get the radioButton to align correctly with text?
New Questions:
Thanks alot for the answers my text is next to my RadioButton now.
Only thing there is no WordWrap, My text is Longer than maximum Size of the RadioButton. How can I get it to wordwrap:
rButton = new QRadioButton();
rButton->setFixedSize(LIST_TEXT_WIDTH_WO_ICON, LIST_TEXT_HEIGHT);
rButton->setStyleSheet("border:none");
rButton->setFont(segoe18Font);
rButton->setText("This is just a test for elementsss of type euh!!!");
rButton->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Preferred);
dropListWidget->setItemWidget(listElement, rButton);

As you may have read, there are two approaches to achieve what you want.
The most flexible one: use a QListView, implement a new delegate and a model if necessary.
Keep using the classic item-based interface (QListWidget) and change the item's widgets either by sub-classing QListWidgetItem or calling QListWidgetItem::setItemWidget.
Since the question points towards the second one, I'll try to provide the simplest item-based solution.
The following piece of code generates the list widget in the picture.
QListWidgetItem *it;
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 1")));
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item 2")));
// .
// .
// .
it = new QListWidgetItem(ui->listWidget);
ui->listWidget->setItemWidget(it, new QRadioButton(tr("Item N")));
where ui->listWidget is a pointer to the QListWidget that holds the items.
I hope this helps. As far as I understand, that's what you need.

Related

How to logically group widgets in QT for easy show/hide?

I'm grouping a set of widgets in a parent and then I control the visibility/flow of these widgets by hiding/showing the parent. Is this a good way to achieve what I'm trying to do? Here is the code:
QVBoxLayout* l = new QVBoxLayout(this);
// .....
QWidget* toolset_frame = new QWidget(this);
{
QVBoxLayout* l = new QVBoxLayout(toolset_frame);
l->addWidget(new QLabel(tr("Stuff")));
this->Toolset = new QLineEdit(toolset_frame);
l->addWidget(this->Toolset);
}
l->addWidget(toolset_frame);
// Call toolset_frame->hide() and this hides everything inside the parent
The problem with this solution is that the children shrink in size slightly, I think this is due to some padding or border in the parent. Ideally the children should appear as if they are not contained in an intermediate object, but rather flow with the parent. In this case the horizontal size of the children should not be affected.
http://doc.qt.io/qt-5/qtwidgets-dialogs-extension-example.html
This example shows that your approach is correct. Using a widget to contain the elements you want to hide, and so on.
If you want the margins/content margins/padding to be less, then change it.
// in finddialog.cpp
extensionLayout->setMargin(0);
To quickly prototype what properties to change to get it to look right, try laying it out in the Qt Designer, and modify the property editor to get the look and feel you want.
Hope that helps.

How to tackle this design obstacle in Qt?

I need to draw a form that would have some options on the left, this can be seen in the following diagram:
Now, when a user clicks on OptionA, a separate related layout would be shown on the right.
and when a user clicks on OptionB, a separate related layout would be shown on the right.
My current design approach for such type of a problem is as follows:
Have a form with a horizontal layout with two frames. One frame has the options, while the other frame would be hosting other forms. So, in my case optionA would have a separate form, say formA, OptionB would have its own form, say formB, and OptionC would have its own form, say formC.
Now when ever a user clicks on OptionA the formA would be displayed inside FrameHost, similarly when user clicks on OptionC, formC would be displayed inside FrameHost. My question is if this is a good approach giving each option an independent form?
My other thought is the opposite which is to have all the forms (A, B and C) layouts inside a separate frame inside one form and when user clicks on OptionA the frame that has FormA content would be made visible while others would be hidden. So, one frame would be shown at a time. What would be the best approach to tackle this kind of a problem?
The first approach is OK, but lacks the concept of QStackWidget. I would personally use QStackedWidget for showing the forms depending on the exclusive radiobutton clicked.
The second approach is very hackish because it is trying to immitate the fact that these forms would be displayed in the same place, yet, it would render them vertically tricking on the visible/hidden property.
I would write the code like this:
MyWidget *formA = new MyWidget;
MyWidget *formB = new MyWidget;
MyWidget *formC = new MyWidget;
QStackedWidget *stackedWidget = new QStackedWidget;
stackedWidget->addWidget(formA);
stackedWidget->addWidget(formB);
stackedWidget->addWidget(formC);
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(stackedWidget);
setLayout(layout);
connect(myButtonGroup, SIGNAL(clicked(int)), SLOT(setCurrentIndex(int)));

Qt get active text box

If I have two text boxes in my main window how can I check which one is active/being used by the user?
You can use QApplication::focusWidget() function to see which widget currently has focus.
Or you can use the QWidget::hasFocus() function to see if your text box has focus.
edit_A = new QTextEdit(this);
edit_B = new QTextEdit(this);
.
.
.
void MyClass::someFunction()
{
if(edit_A->hasFocus())
//edit_A is being used
else if(edit_B->hasFocus())
//edit_B is being used
}
The previous answer is right but just in case you want to monitor more components you can use
QWidget * QApplication::focusWidget ()
to get widget with focus.
This approach will allow you to make cleaner code if you wanted to have more widgets monitored. Instead of having a ladder of ifs just use some kind of look-up table to choose what action to take.

Using images in QListWidget, is this possible?

I am using QT to create a chat messenger client. To display the list of online users, I'm using a QListWidget, as created like this:
listWidget = new QListWidget(horizontalLayoutWidget);
listWidget->setObjectName("userList");
QSizePolicy sizePolicy1(QSizePolicy::Preferred, QSizePolicy::Expanding);
sizePolicy1.setHorizontalStretch(0);
sizePolicy1.setVerticalStretch(0);
sizePolicy1.setHeightForWidth(listWidget->sizePolicy().hasHeightForWidth());
listWidget->setSizePolicy(sizePolicy1);
listWidget->setMinimumSize(QSize(30, 0));
listWidget->setMaximumSize(QSize(150, 16777215));
listWidget->setBaseSize(QSize(100, 0));
listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
Users are shown by constantly refreshing the list, like this: (Note: There are different channels, with different userlists, so refreshing it is the most efficient thing to do, as far as I know.)
void FMessenger::refreshUserlist()
{
if (currentPanel == 0)
return;
listWidget = this->findChild<QListWidget *>(QString("userList"));
listWidget->clear();
QList<FCharacter*> charList = currentPanel->charList();
QListWidgetItem* charitem = 0;
FCharacter* character;
foreach(character, charList)
{
charitem = new QListWidgetItem(character->name());
// charitem->setIcon(QIcon(":/Images/status.png"));
listWidget->addItem(charitem);
}
}
This has always worked perfectly. The line that I commented out is the one I have problems with: my current goal is to be able to display a user's online status with an image, which represents whether they are busy, away, available, etc. Using setIcon() does absolutely nothing though, apparently; the items still show up as they used to, without icons.
I'm aware that this is probably not the way this function needs to be used, but I have found little documentation about it online, and absolutely no useful examples of implementations. My question is, can anybody help me with fixing this problem?
This is how you may conduct your debugging:
Try the constructor that has both icon and text as arguments.
Try to use that icon in another context to ensure it is displayable (construct a QIcon with same argument and use it elsewhere, e.g. QLabel!).
Use icon() from the QListWidgetItem to receive back the icon and then look at that QIcon.
Create a new QListWidget, change nothing, and ordinarily add some stock items in your MainWidget's constructor. See if the icons show up there.

How to set the line where a QToolBar is displayed?

I would like to ask if anyone knows how to display 2 QToolBars in two lines, one on top of the other? I found the class QStyleOptionToolBar, but I don't know how to use it...
It is easy to drag one toolbar with the mouse to be placed below the other, so I think there must be a way how this can be done from the source code as well...
Any hint would be appreciated!
Claus
Try calling QMainWindow::addToolBarBreak(Qt::ToolBarArea) in between adding the two tool bars.
I think that when you add the 2 toolbar's if you place them in the same area, they'll stack automatically:
QMainWindow *mainWin = get_main(); // however you get it
mainWin->addToolBar(Qt::TopToolBarArea, new QToolBar);
mainWin->addToolBar(Qt::TopToolBarArea, new QToolBar);
This should produce 2 toolbars, both at the top.
If you want toolbars to be in two lines you should do it in following manner:
firstToolbar = new QToolBar(this);
secondToolbar = new QToolBar(this);
addToolBar(Qt::TopToolBarArea, firstToolbar);
addToolBarBreak();
addToolBar(Qt::TopToolBarArea, secondToolbar);
addToolBarBreak accepts Qt::ToolBarArea so you can specify for which area you would like to add the break. By default it is Qt::TopToolBarArea.