Radio Button never appears - c++

With the following code to create radio buttons in a group box I get no results. A bit of background : in my display every objects has multiple variants and the user must choose which one to display (or all). What update/refresh command did I miss, or what is wrong ?
for(int i = 0; i < MAX_NUM_VARIANTS+1; i++)
{
m_variantButtons[i] = new QRadioButton();
m_variantButtons[i]->setText(QString::number(i));
m_variantButtons[i]->setObjectName(QString("m_pbDisplayVariant%1").arg(i));
QSizePolicy sizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding);
sizePolicy.setHorizontalStretch(0);
sizePolicy.setVerticalStretch(0);
sizePolicy.setHeightForWidth(true);
m_variantButtons[i]->setSizePolicy(sizePolicy);
((QGridLayout*)(ui.m_groupBoxVariants->layout()))->addWidget(m_variantButtons[i], 1, i);
((QGridLayout*)(ui.m_groupBoxVariants->layout()))->update();
connect(m_variantButtons[i], SIGNAL(clicked(bool)), this, SLOT(updateDisplaySettings()));
m_variantButtons[i]->setVisible(true);
}
((QWidget*)(ui.m_groupBoxVariants))->updateGeometry();

Related

connect dynamically created buttons in qt5

I have a scenario where I am asking the user for a number between 1 and 10 and creating that number of buttons of the type QPushButton. I then want to create a function such that when I click the button the number on the button gets printed.
Just use a lambda function like this:
for (int i = 1; i < numButtons; i++)
{
QPushButton *btn = new QPushButton(...);
connect(btn, &QPushButton::clicked, [=]() {
// Do something with 'i'
}
}

Second time creating ui elements will be invisible

I have a vector.
QVector<QPushButton*> buttonVector;
With this function I add dynamically buttons to the UI.
void MainWindow::createButton()
{
for(int x = 0; x < list.length(); x++)
{
QPushButton *button = new QPushButton(this);
button->setText(QString::number(list.at(x)->ID()));
button->setGeometry(x_cor,y_cor,50,50);
//button->setVisible(true);
buttonVector.append(button);
}
}
At the start of my program this function triggers and works perfectly, every button is showing.
But after I recreate every button with this function after I press a button, they are all invisible if I don't add the line:
button->setVisible(true);
Why is that? At the beginning they are all visible without this line.

QListWidget horizontal scrollbar causes selection to go out of view

I've asked this question previously and a wonderful person lead me to a decent workaround for the issue. However, I am hoping to see if there is a better solution. One that actually prevents any shifting in my QListWidget entirely.
Working demo example
ListDemo zipfile
http://nexrem.com/test/ListDemo.zip
ListDemo cpp code
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
myListWidget = new QListWidget();
/*
* The signal-slot below is a temporary workaround for the shifting issue.
* This will ensure that the item selected remains in view,
* This is achieved by forcing the item to be in the center of the window;
* however, this has an undesired side-effect of visible 'jumping' as the list
* scrolls to center the item.
*/
//connect (myListWidget, SIGNAL(itemClicked(QListWidgetItem*)), this,
// SLOT(scrollToItem(QListWidgetItem*)));
for (int i = 0; i <= 1000; ++i)
{
QListWidgetItem * myItem = new QListWidgetItem(myListWidget);
QString text("");
for (int i = 0; i <= 40; ++i)
{
text.append("W");
}
myItem->setText(text + QString::number(i));
}
for (int i = 0; i <= 1000; ++i)
{
if (i%2)
myListWidget->item(i)->setHidden(true);
}
auto selected = myListWidget->selectedItems();
if (selected.size() == 1)
{
myListWidget->scrollToItem(selected.front());
}
setCentralWidget(myListWidget);
}
void MainWindow::scrollToItem(QListWidgetItem * item)
{
std::cout << "Scrolling to the item." << std::endl;
myListWidget->scrollToItem(item, QAbstractItemView::PositionAtCenter);
}
The problem:
Whenever I have a QListWidget with horizontal scrollbars and hidden rows present, I get an undesired behaviour that whenever a user clicks on an item, it disappears from view, and the entire list shifts down.
In the example above, I've hidden every other row, to demonstrate this behaviour.
The workaround:
Workaround is having a signal-slot connection that forces the selected item to be scrolled back into view and positioned at the center.
Note, that I have to use PositionAtCenter as EnsureVisible does not work. It thinks the item is visible when it is out of view.
This workaround is acceptable; however, there is visible 'jump' when your selection is forced to be positioned at the center. This is an undesirable side effect.
At this point I am not sure whether this is a QT bug (I don't think having horizontal scrollbar should force your selection out of view) or my code is missing something vital.
The Fix:
As per #G.M.'s comment, all that was missing is myListWidget->setAutoScroll(false);
As mentioned in the comment...
To prevent automatic scrolling on selection disable the autoScroll property. So, in the example code provided do...
myListWidget->setAutoScroll(false);
Note that this property also has an effect when items are dragged over the list view so if you want your list view to act as a drop site then you will probably want to re-enable this property when you get a QDragEnterEvent.

How to get current row of QTableWidget if I clicked on its child?

I have created a QTableWidget in which I've used setCellWidget(QWidget*). I've set QLineEdit in the cell widget. I've also created a delete button and clicking that button sends a signal to the function deleteRow. I've also used a function currentRow() to get the current row, but it returns -1 because of the QLineEdit. The code snippet is below.
void createTable() {
m_table = new QTableWidget(QDialog); //member variable
for (int i = 0; i < 3; i++)
{
QLineEdit *lineEdit = new QLineEdit(m_table);
m_table->setCellWidget(i, 0, lineEdit);
}
QPushButton *deleteBut = new QPushButton(QDiaolg);
connect(deleteBut, SIGNAL(clicked()), QDialog, SLOT(editRow()));
}
editRow() {
int row = m_table->currentRow(); // This gives -1
m_table->remove(row);
}
In above scenario I click in the QLineEdit and then click on the button delete. Please help me out with a solution.
Just tried it here, it seems that currentRow of the table returns -1 when clicking the button right after program start, and when first selecting a cell, then selecting the QLineEdit and then clicking the button, the correct row is returned.
I would do the following as a workaround: Save the row number in the QLineEdit, e.g. by using QObject::setProperty:
QLineEdit *lineEdit = new QLineEdit(m_table);
lineEdit->setProperty("row", i);
m_table->setCellWidget(i, 0, lineEdit);
Then, in the editRow handler, retrieve the property by asking the QTableWidget for its focused child:
int row = m_table->currentRow();
if (row == -1) {
if (QWidget* focused = m_table->focusWidget()) {
row = focused->property("row").toInt();
}
}
The accepted solution, as is, would not work if rows might get deleted while the program runs. Thus the approach would require to update all the properties. Can be done, if this is a rare operation.
I got away with an iteration approach:
for(unsigned int i = 0; i < table->rowCount(); ++i)
{
if(table->cellWidget(i, relevantColumn) == QObject::sender())
{
return i;
}
}
return -1;
Quick, dirty, but worked, and in my case more suitable, as rows got deleted often or changed their positions, only buttons in the widget were connected to the slot and the slot was never called directly. If these conditions are not met, further checks might get necessary (if(QObject::sender()) { /* */ }, ...).
Karsten's answer will work correctly only if QLineEdit's property is recalculated each time a row is deleted, which might be a lot of work. And Aconcagua's answer works only if the method is invoked via signal/slot mechanism. In my solution, I just calculate the position of the QlineEdit which has focus (assuming all table items were set with setCellWidget):
int getCurrentRow() {
for (int i=0; i<myTable->rowCount(); i++)
for (int j=0; j<myTable->columnCount(); j++) {
if (myTable->cellWidget(i,j) == myTable->focusWidget()) {
return i;
}
}
return -1;
}

Qt - how to customizing QTableWidget's checkbox

I've tried to set some style for check box for Qt. I've already knew that QtTableWidget has QCheckbox. but problem is I have no idea how to set style for checkbox of QtTableWidget.
QTableWidgetItem *checkBoxItem = new QTableWidgetItem();
checkBoxItem->setCheckState(Qt::Unchecked);
table->setItem(row, column, checkBoxItem);
When I use setStyleSheet for checkBox :
checkBoxItem->setStyleSheet("...");
I have an error:
'class QTableWidgetItem' has no member named 'setStyleSheet'
Edited: I want to do some operation with checkBox. Here is complete code for first QTableWidgetItem:
for (int i = 0; i < 4; ++i)
m_tableWidget->setRowHeight(i, 3 * em);
QTableWidgetItem *item1 = new QTableWidgetItem(tr("Show Message Preview"));
if (CGlobalZone::m_showMsgPreview)
item1->setCheckState(Qt::Checked);
else
item1->setCheckState(Qt::Unchecked);
item1->setFlags(Qt::ItemIsEnabled);
m_tableWidget->setItem(0, 0, item1);
You can style the indicators with QAbstractItemView::indicator { ... } (eg QTableView::indicator:checked, QTableView::indicator:unchecked etc).
You can't apply style directly to the QTableItemWidget, but you can put a stylesheet on the QTableWidget itself or a parent of it.
Assuming, you already have a table widget with at least one column and at least one row:
QCheckBox *cb = new QCheckBox(tr("Check me"));
cb->setStyleSheet("background-color: rgb(0, 85, 0);");
tableWidget->setCellWidget(0, 0, cb);