I am setting up an user manager and i need to implement User statue policy. I have a list of users in QTreeWidget and each user has its own QTreeWidgetItem. User has some atributes (ban, hasPassword...) and I want to show it instantly in list.
bool cUserManager::addUser2TreeWidget(cUser* user) {
if (!user) return false;
QTreeWidgetItem* item_user = 0;
item_user = new QTreeWidgetItem(item_AVAILABLE);
item_user->setCheckState(0,Qt::Unchecked);
item_user->setText(0, user->getName());
QWidget *userStateIcons = new QWidget();
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(new QPushButton(QIcon(PATH_ICON_BAN),""));
hLayout->addWidget(new QPushButton(QIcon("PATH_ICON_LOCK"),""));
userStateIcons->setLayout(hLayout);
ui.treeUsers->setItemWidget(item_user,1,userStateIcons);
return true;
Output:
I expect to see the:
Checkbox, Icon(if user is banned), Icon(if user has password),Icon(if user has admin rights), text (User name).
With buttons it is very hard to style it(set width, height, flat button...). Is there any proper and easier way to just insert only pixmap?
Thank you for advises and tips.
Related
I have an editable QComboBox. I add some items to it with associated user data.
QComboBox *myCB = new QComboBox;
myCB->setEditable(true);
myCB->addItem("Item1", "1");
myCB->addItem("Item2", "2");
myCB->addItem("Item3", "3");
When an item is selected from the combo box, I want to get its associated user data
But user types in something into the combo box, I just want to get the typed string.
if (selected_from_combobox)
return myCB->itemData(myCB->currentIndex()).toString();
else if (typed_by_user)
return myCB->currentText();
How do I differentiate between 2 cases?
You have to use 2 signals like this:
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
QComboBox *myCB = new QComboBox(this);
myCB->setEditable(true);
myCB->addItem("Item1", "1");
myCB->addItem("Item2", "2");
myCB->addItem("Item3", "3");
connect(myCB,SIGNAL(currentIndexChanged(int)),this,SLOT(indexChanged(int)));
connect(myCB,SIGNAL(currentTextChanged(QString)),this,SLOT(textChanged(QString)));
}
I would like to use the standard JavaFX Alert class for a confirmation dialog that includes a check box for "Do not ask again". Is this possible, or do I have to create a custom Dialog from scratch?
I tried using the DialogPane.setExpandableContent() method, but that's not really what I want - this adds a Hide/Show button in the button bar, and the check box appears in the main body of the dialog, whereas I want the check box to appear in the button bar.
Yes, it is possible, with a little bit of work. You can override DialogPane.createDetailsButton() to return any node you want in place of the Hide/Show button. The trick is that you need to reconstruct the Alert after that, because you will have got rid of the standard contents created by the Alert. You also need to fool the DialogPane into thinking there is expanded content so that it shows your checkbox. Here's an example of a factory method to create an Alert with an opt-out check box. The text and action of the check box are customizable.
public static Alert createAlertWithOptOut(AlertType type, String title, String headerText,
String message, String optOutMessage, Consumer<Boolean> optOutAction,
ButtonType... buttonTypes) {
Alert alert = new Alert(type);
// Need to force the alert to layout in order to grab the graphic,
// as we are replacing the dialog pane with a custom pane
alert.getDialogPane().applyCss();
Node graphic = alert.getDialogPane().getGraphic();
// Create a new dialog pane that has a checkbox instead of the hide/show details button
// Use the supplied callback for the action of the checkbox
alert.setDialogPane(new DialogPane() {
#Override
protected Node createDetailsButton() {
CheckBox optOut = new CheckBox();
optOut.setText(optOutMessage);
optOut.setOnAction(e -> optOutAction.accept(optOut.isSelected()));
return optOut;
}
});
alert.getDialogPane().getButtonTypes().addAll(buttonTypes);
alert.getDialogPane().setContentText(message);
// Fool the dialog into thinking there is some expandable content
// a Group won't take up any space if it has no children
alert.getDialogPane().setExpandableContent(new Group());
alert.getDialogPane().setExpanded(true);
// Reset the dialog graphic using the default style
alert.getDialogPane().setGraphic(graphic);
alert.setTitle(title);
alert.setHeaderText(headerText);
return alert;
}
And here is an example of the factory method being used, where prefs is some preference store that saves the user's choice
Alert alert = createAlertWithOptOut(AlertType.CONFIRMATION, "Exit", null,
"Are you sure you wish to exit?", "Do not ask again",
param -> prefs.put(KEY_AUTO_EXIT, param ? "Always" : "Never"), ButtonType.YES, ButtonType.NO);
if (alert.showAndWait().filter(t -> t == ButtonType.YES).isPresent()) {
System.exit();
}
And here's what the dialog looks like:
I'm working on Adobe Flash CS5 and on actionscript 3 - My question is how can I make a button that only becomes available to click on once the four other buttons have been clicked and the scenes have been accessed?
Let's assume that your 5 buttons are always available on the timeline and they are at the top document level (if not, we need to make some adjustments). Let's also assume that, from your question, you're not ready for the best practice of a document Class file, so this will be written as timeline script.
//assumes the 4 buttons are on the stage, named button1, button2, etc.
var prerequisiteButtons:Array = [button1, button2, button3, button4];
var prerequisitesClicked:Array /*of Boolean*/ = [];
coyButton.enabled = false;//your fifth button, who plays hard to get
//loop through and listen for clicks on your prerequisite buttons
for each (var button:DisplayObject in buttons) {
button.addEventListener(MouseEvent.CLICK, checkAllClicked);
}
//check to see if all the buttons have been clicked
function checkAllClicked(e:Event):void {
//find the button in the prerequisite buttons array
var buttonIndex:int = prerequisiteButtons.indexOf(e.currentTarget);
//set the matching index to true in the array that keeps track of what has been clicked
prerequisitesClicked[buttonIndex] = true;
//count how many of them have been clicked
var prerequisiteDoneCount:int = 0;
for (var i:int = 0; i< prerequisitesClicked.length; i++) {
if (prerequisitesClicked[i]) prerequisiteDoneCount++;
}
//if all the buttons have been clicked, enable the button (may also want to add a listener here)
if (prerequisiteDoneCount==prerequisiteButtons.length) coyButton.enabled = true;
}
for (iss = 0; iss < listOfProductIds2.length; iss++)
{
// Alert.show(listOfProductIds2[iss]);
var productMain:VBox=new VBox();
var p1:HBox=new HBox();
var l1:Label=new Label();
var b1:Button=new Button();
var spacer:Spacer=new Spacer();
spacer.width=300;
b1.label="Remove";
b1.setConstraintValue("id","");
b1.addEventListener(MouseEvent.CLICK,removeProduct);
l1.text="Product "+iss;
p1.setActualSize(500,500);
p1.addChild(l1);
p1.addChild(spacer);
p1.addChild(b1);
productMain.addChild(p1);
}
function removeProduct(event:MouseEvent):void
{
// How do i know which button is clicked
}
Use event.currentTarget (instead of event.target) because event.target might be the Label component or some styling component within the button, but currentTarget is assured to be the object with which the listener was registered.
To get a handle to the button that was clicked you can just cast the currentTarget to a button.
function removeProduct(event:MouseEvent):void
{
var b1:Button = Button(event.currentTarget);
}
The method setConstraintValue is for setting layout constraints, not setting id. The id property is used by mxml for creating variable names for objects. You can get/set id as you would get/set any other property (say width) - but neither have I seen anyone doing that nor do I see any need to do that in the first place.
event.target should point to the Button you clicked on, shouldn't it ? However you should probably give ids to the buttons to be able to differenciate them (since you create them dynamically.)
Look at event.target.
If ids are assigned dynamically as in the example given b1.id = "button_" + listOfProductIds2[iss]
Then the function that processes the click event would look at the currenttarget, and what I usually do is do a string replace on the part of the id that you know is not dynamic like "button_" with "", which leaves you with the name of the product.
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