I need to use QTableWidget with checkboxes instead of text in items. Checkbox must be in the center of item.
Examples which I tried work while checkbox is checked. If I uncheck checkbox it disapeares.
You can set the checkbox to be centered with this code:
QWidget *pWidget = new QWidget();
QCheckBox *pCheckBox = new QCheckBox();
QHBoxLayout *pLayout = new QHBoxLayout(pWidget);
pLayout->addWidget(pCheckBox);
pLayout->setAlignment(Qt::AlignCenter);
pLayout->setContentsMargins(0,0,0,0);
pWidget->setLayout(pLayout);
pMyTableWidget->setCellWidget(0,0,pWidget);
(I don't know if I understood you well here) And if you want to make your checkbox disappear when you uncheck it, you need to connect clicked signal of checkbox to a slot, that will make your checkbox invisible. Use connect method like this:
connect(checkbox,SIGNAL(clicked()),this,SLOT(checkboxClicked()));
You need to create slot checkboxClicked where you will be checking if the checkbox is checked or not. If not then you have to set it invisible. Example:
QCheckBox* Chb = qobject_cast<QCheckBox *>(QObject::sender());
if(!Chb->checked())
Chb->setVisible(false);
Related
I have created a Qt project in which I'm using a widget and it does not support any menu in the designer's class, so it should be done programmatically. I succeeded in creating the menu and adding the items but I'm struggling in assigning any action for the menu items...
That's what I've done so far:
QMenuBar* menuBar = new QMenuBar();
QMenu *fileMenu = new QMenu("File");
menuBar->addMenu(fileMenu);
fileMenu->addAction("Save");
fileMenu->addAction("Exit");
QAction* newAct = new QAction(tr("&New"), this);
newAct->setShortcuts(QKeySequence::New);
newAct->setStatusTip(tr("Exit"));
connect(newAct, &QAction::triggered, this, &MainWindow::on_action_triggered);
this->layout()->setMenuBar(menuBar);
But no action is triggered when I press The Exit item
addAction creates QAction and returns pointer to it, you dont need to create it explicitly, but when you do you must add item to menu with addAction, setting parent is not enough fileMenu->addAction(newAct);.
I need to put a QPushButton inside a QMenu. Is it possible and, if so, then how?
I want to achieve something like this:
QWidgetAction is what you are looking for. This is what is on qt docs
The QWidgetAction class extends QAction by an interface for inserting custom widgets into action based containers
So basically it gives a custom UI to QAction according to what QWidget you pass to it.
I have used QWidgetAction to show checkbox as QMenu items.
QCheckBox *chkBox = new QCheckBox(menu);
chkBox ->setText("MyCheckBox");
QWidgetAction *chkBoxAction= new QWidgetAction(menu);
chkBoxAction->setDefaultWidget(chkBox);
menu->addAction(chkBoxAction);
You can then handle signals from checkbox accordingly.
If you only want a menu item to have a state, you may use Checkable property of QAction:
rotateAct = new QAction(QIcon(":/images/Mouse/Rotate.png"), tr("&Rotate"), this);
rotateAct->setCheckable(true);
I want to add a QPushButton inside ListItm, so I have implemented the code as given below. But the button comes in the middle of the list, actually I want it at the lower end of the list item. How is it possible. Also that button click event is not working. Actually I want to disable the item click event directly and by clicking the button inside the QListWidgetItem, I want to enable the item click event. But I am not able to perform this operation. How to do this? I have used the following code snippet:
list=new QListWidget(this);
// list->setStyleSheet("* { background-color:rgb(0,0,0); padding: 10px ; color:rgb(255,255,255)}");
list->setGeometry(0,61,360,475);
list->setSortingEnabled(true);
//connect(list,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(ItemClicked(QListWidgetItem*)));
item=new QListWidgetItem();
item->setIcon(QIcon(":/images/Icon.png"));
item->setText("Item1");
item->setSizeHint(QSize(80,80));
item->setBackgroundColor(QColor(200,255,100));
list->addItem(item);
QPushButton *but = new QPushButton(">");
but->setMaximumSize(50,80);
but->setFlat(true);
// but->setGeometry(QRect(500,100,100,100));
but->setStyleSheet("background: transparent; border: none");
QHBoxLayout *layout= new QHBoxLayout();
layout->addWidget(but);
QWidget *widget = new QWidget();
widget->setLayout(layout);
item->setSizeHint(widget->sizeHint());
list->setItemWidget(item, widget);
connect(but, SIGNAL(clicked()), this, SLOT(ItemClicked()));
#if defined(Q_WS_S60)
list->showMaximized();
#else
list->show();
#endif
ItemClicked()
{
int Index = list->currentIndex().row();//Always getting this Index as -1
}
Please have a look on the above code and provide your suggestions.
Thanks...
Reconsider using QPushButton as "click" handle for listwidget. There is a reason why QListWidgetItem is not QObject. QObjects are somewhat "heavy", because of all metadata structures they hold. That's why Qt is not using QObjects in data oriented lists like QListWidgetItem.
About your problem. You will get always -1, until you won't select item by NOT clicking on it's button part, but on item. That's because QPushButton is taking focus away, and doesn't pass click event down to QListWidgetItem. So it may even happend, that you select item with idx = 3, click on button of item with idx = 1 and will get in your slot idx 3.
Actualy, as for me you're performing your whole taks totaly wrong. First of all, I would use QTreeWidget, for multicolumns. Secondly, I would "implement" custom item delegate, to draw "button", and I would set it as delegate for column 1. Then I would catch "click event" normally, but react only for column 1.
| column 0 (actual data exposition) | column 1 (custom delegate, draw button)
I have a QGroupBox with a couple of QRadioButtons inside of it and in certain cases I want all radio buttons to be unchecked. Seems that this is not possible when a selection has been made. Do you know of a way I could do this or should I add a hidden radiobutton and check that onen to get the desired result.
You can achieve this effect by temporarily turning off auto exclusivity for all your radio buttons, unchecking them, and then turning them back on:
QRadioButton* rbutton1 = new QRadioButton("Option 1", parent);
// ... other code ...
rbutton1->setAutoExclusive(false);
rbutton1->setChecked(false);
rbutton1->setAutoExclusive(true);
You might want to look at using QButtonGroup to keep things tidier, it'll let you turn exclusivity on and off for an entire group of buttons instead of iterating through them yourself:
// where rbuttons are QRadioButtons with appropriate parent widgets
// (QButtonGroup doesn't draw or layout anything, it's just a container class)
QButtonGroup* group = new QButtonGroup(parent);
group->addButton(rbutton1);
group->addButton(rbutton2);
group->addButton(rbutton3);
// ... other code ...
QAbstractButton* checked = group->checkedButton();
if (checked)
{
group->setExclusive(false);
checked->setChecked(false);
group->setExclusive(true);
}
However, as the other answers have stated, you might want to consider using checkboxes instead, since radio buttons aren't really meant for this sort of thing.
If you're using QGroupBox to group buttons, you can't use the setExclusive(false) function to uncheck the checked RadioButton. You can read about it in QRadioButton section of QT docs. So if you want to reset your buttons, you can try something like this:
QButtonGroup *buttonGroup = new QButtonGroup;
QRadioButton *radioButton1 = new QRadioButton("button1");
QRadioButton *radioButton2 = new QRadioButton("button2");
QRadioButton *radioButton3 = new QRadioButton("button3");
buttonGroup->addButton(radioButton1);
buttonGroup->addButton(radioButton2);
buttonGroup->addButton(radioButton3);
if(buttonGroup->checkedButton() != 0)
{
// Disable the exclusive property of the Button Group
buttonGroup->setExclusive(false);
// Get the checked button and uncheck it
buttonGroup->checkedButton()->setChecked(false);
// Enable the exclusive property of the Button Group
buttonGroup->setExclusive(true);
}
You can disable the exclusive property of the ButtonGroup to reset all the buttons associated with the ButtonGroup, then you can enable the Exclusive property so that multiple button checks won't be possible.
I have a QTableView and a QStandardItemModel. Is there have a column can contain checkboxes that are user editable without using delegates or using the abstract model classes? It is not that I can't do it, I just want to minimize the code, I would find it overkill for simple check boxes.
By using model.setData(index, Qt::Unchecked,Qt::CheckStateRole) this creates the checkbox but it is not user editable (text beside checkbox is).
I used modelTX.setData(index, FALSE) but this creates a combo box containing True and False.
I'll try setItemData.
pls, check if the following example would work for you:
QStandardItemModel* tableModel = new QStandardItemModel();
// create text item
tableModel->setItem(0, 0, new QStandardItem("text item"));
// create check box item
QStandardItem* item0 = new QStandardItem(true);
item0->setCheckable(true);
item0->setCheckState(Qt::Checked);
item0->setText("some text");
tableModel->setItem(0, 1, item0);
// set model
ui->tableView->setModel(tableModel);
hope this helps, regards