I have a method to automatically add checkable actions on a menu and it's working perfectly. I just like to know how to unchecked all these actions automatically. I know that it's possible with ui->action_item->setChecked(false) but in this case I should know and write each action name in the code however it's not viable for me.
Pseudo-code:
void MainWindow::AddNewActions() {
QActionGroup *my_action_group = new QActionGroup(this);
my_action_group->setExclusive(true);
foreach (..) {
QAction *my_new_action = new QAction(description, my_action_group);
my_new_action->setCheckable(true);
my_new_action->setData(action_name);
ui_->my_menu->addAction(my_new_action);
}
}
I implemented a simplest solution:
QList<QMenu*> menus;
menus = ui->menuBar->findChildren<QMenu*>();
foreach (QMenu* menu, menus)
{
foreach (QAction* action, menu->actions())
{
if(action->isCheckable()){
action->setChecked(false);
}
}
}
You need to get a list of actions, and then uncheck each of them. The list of actions is simply the list of QAction children of the menu - but that's if they don't belong to an action group. That would be, then:
void Class::method2() {
auto actions = std::as_const(ui_->my_menu->findChildren<QAction*>());
for (auto *action : actions)
action->setChecked(false);
}
If you want to find actions in a particular action group, you need to name the action group with a unique name, to refer to it later:
static const QString k_my_action_group{QLatin1String("my_action_group")};
void Class::method1() {
auto *my_action_group = new QActionGroup{this};
my_action_group->setObjectName(k_my_action_group);
...
}
void Class::method2() {
if (auto *group = findChild<QActionGroup*>(k_my_action_group))
if (auto *action = group->checkedAction())
action->setChecked(false);
}
If you have multiple groups, they can share the same name, and then you'd iterate them too:
void Class::method2() { // if somehow more than one action can be checked
auto groups = std::as_const(findChildren<QActionGroup*>(k_my_action_group));
for (auto *group : groups)
if (auto *action = group->checkedAction())
action->setChecked(false);
}
If your compiler doesn't implement std::as_const yet, use qAsConst instead. The const-casting is the unfortunate effect of the implicit shared design of Qt containers.
Object names referred to in multiple places should be used as string constants with symbolic names. This makes typos detectable at compile time, vs. at runtime.
Related
Problem description
I have a treeview (TornadoFX) which gets populated with 3 Observable Lists. If I mutate the 3 lists (first clear them than add new content) the treeview only contains the content of one of the Observable Lists.
The interpretation of my debugging
The treewiev actualises itself in the moment the first List haves its new content and the other 2 are still empty. It does not actualise itself as soon as the other 2 Lists gain their new content.
What I have tried so far
calling treeview.root.children.clear() before I mutate the List to empty the treewiev (same outcome)
calling treeview.refresh() after all Lists have been set (same outcome)
My (more or less pseudo) Code
{//Classes
R extends X
A(val name :String) extends X
B(val name :String, val A :String) extends X
C(val B :String) extends X
}
{//this is how the Lists get their content each time
private val aList = observableListOf<A>()
private val bList = observableListOf<B>()
private val cList = observableListOf<C>()
fun setLists(){
a.clear()
b.clear()
c.clear()
//...
a.add(...)
b.add(...)
c.add(...)
}
}
{
treeview<X> {
root = TreeItem(R)
populate{ parent ->
if (parent == root){
aList
} else if (parent.value is A){
bList.filter { it.A == parent.value.name } //works always
}else if (parent.value is B){
cList.filter { it.B == parent.value.name } //works at first start but not after set setLists() get recalled
}else {
emptyList<X>() //not sure if this is really needed but the compiler needs something here
}
}
}
}
Side Question
Ignore this Question if you want: Intelij is able to run my TornadoFX application (from main or from App with the TornadoFX Plugin), but if I try to export it it doesnt run (neither the jar nor the jnlp). Even if I select TornadoFX and all the other Libraries in the Artifact menu. BUT: if I select Native Bundle -> image in the JavaFX tab, the resulting exe (and only the exe) is working. I also tried to create a shadowJar with com.github.johnrengelman.shadow 5.2.0 Plugin but that also didnt work. So how can I export the Program as a jar (a fatjar including TornadoFX in best case)?
Well I don't know if it's the perfect solution but I simply clear the children and populate the treeview again. It works, but if that is the intended way it's not as "reactive" as I thought it is.
I have a program which when it runs, at first the user is asked to initialize the system. In that question form, there are 3 checkboxes that the user can check them for specific person or every persons and the system initializes the items related to that checkbox for the person(s).
When a checkbox is selected, a specific function and subsequently the specific class is called and initialization is done.
In the mainwindow.cpp I have:
InitializeDialog *id=new InitializeDialog;
connect(id,&InitializeDialog::initializeSignal,this,&MainWindow::initializeSlot);
id->exec();
id is the question form which has 3 checkboxes in it. And:
void MainWindow::initializeSlot(QStringList persons, bool interests, bool plots, bool graphs)
{
initializeMBox->setWindowTitle(tr("Initializing System")+"...");
initializeMBox->setText(tr("Please wait until initialization has been done") + ".<br>");
initializeMBox->show();
initializeMBox->setStandardButtons(0);
if (interests)//checkbox 1 is checked
initializeInterests(persons);
if (plots)//checkbox 2 is checked
initializePlots(persons);
if(graphs)//checkbox 3 is checked
initializeGraphs(persons);
initializeMBox->setStandardButtons(QMessageBox::Ok);
}
And again:
void MainWindow::initializeInterests(QStringList persons)
{
for(int p=0;p<persons_comboBox_->count();p++)
{
persons_comboBox_->setCurrentIndex(p);
if (persons.contains(persons_comboBox_->currentText()))
{
//..
//create a specific class object and some specific functions
//..
//*
initializeMBox->setText(initializeMBox->text() + "<div><img src=\":/tickIcon.png\" height=\"10\" width=\"10\">" + " " + tr("Interests analyzed for the persons") + ": " + persons_comboBox_->currentText() + ".</div>");
}
}
}
initializePlots and initializeGraphs are similiar to initializeInterests.
The problem starts from here:
I want to show a message after initialization for every person (as I mentioned by star in initializeInterests) but my initializeMBox (is a QMessageBox) does not show the message continuously and when all persons are initialized, all messages are shown suddenly. It should be noted that I see my initializeMBox is getting bigger but it seems that my QMessageBox is Freezed.
I can't use QtConcurrent::run because my QMessageBox is updated from mainwindow (and so from the base thread) by the line that I mentioned by star.
How can I have a QMessageBox which be updated continuously?
Don't reenter the event loop. Replace id->exec() with id->show(). Manage the dialog's lifetime - perhaps it shouldn't be dynamically created at all.
Don't block in initializeInterests. Instead of changing the combo box, get its data, send it out to an async job, set everything up there, then send the results back.
Pass containers by const reference, not value.
Don't create strings by concatenation.
If the input persons list is long, sort it to speed up look-ups.
For example:
class StringSignal : public QObject {
Q_OBJECT
public:
Q_SIGNAL void signal(const QString &);
};
void MainWindow::initializeInterests(const QStringList &personsIn) {
auto in = personsIn;
std::sort(in.begin(), in.end());
QStringList persons;
persons.reserve(in.size());
for (int i = 0; i < persons_comboBox_->count(); ++i) {
auto const combo = persons_comboBox->itemText(i);
if (std::binary_search(in.begin(), in.end(), combo))
persons << combo;
}
QtConcurrent::run([persons = std::move(persons), w = this](){
StringSignal source;
connect(&source, &StringSignal::signal, w, [w](const QString & person){
w->initalizeMBox->setText(
QStringLiteral("%1 <div><img src=...> %2: %3.</div>")
.arg(w->initalizeMBox->text())
.arg(tr("Interests analyzed for the person"))
.arg(person)
);
});
for (auto &person : persons) { // persons is const
// create a specific object etc.
QThread::sleep(1); // let's pretend we work hard here
source.signal(person);
}
});
}
The creation of the "specific objects" you allude to should not access anything in the gui thread. If it doesn't - pass a copy of the required data, or access it in a thread-safe manner. Sometimes it makes sense, instead of copying the data, move it into the worker, and then when the worker is done - move it back into the gui, by the way of a lambda.
I have a form, when i click on my button.It adds to my table A (what my factbox shows)is it possible to refresh the factbox with X++ code? I can't figure out how to refresh my infopart or query which factbox uses.
For an infopart you can call an update of the data source of the infopart's form run:
void clicked()
{
PartList partList;
int i;
FormRun infoPartFormRun;
FormDataSource infoPartDataSource;
super();
partList = new PartList(element);
for (i = 1; i <= partList.partCount(); i++)
{
infoPartFormRun = partList.getPartById(i);
if (infoPartFormRun.name() == identifierStr(MyInfoPart))
{
infoPartDataSource = infoPartFormRun.dataSource();
if (infoPartDataSource)
{
infoPartDataSource.research();
}
}
}
}
I added the check for the infoPartDataSource because I first tested this with a cue group fact box, which does not have a data source (or at least I could not figure out how to get the data source of one of the cues in the cue group and since you asked for an infopart fact box, I did not investigate further).
Update: The issue seems to be popular at the moment, Martin DrĂ¡b also wrote in his blog about it: Refreshing form parts
I create a menu dynamically. I add several checkable actions into one menu. Sometimes actions may have the same text that user sees. It's up to user (actually user adds commands into menu).
The problem is in this case clicking works wrong. If I click on the first action (from 2 with the same texts) everything is good but if I click on the second one, both actions are selected. I don't understand why. The code where actions have been created is here:
for (int i = 0; i< currentList.size(); i++)
{
QString lanKey = currentList.at(i)->Language->toString();
QAction* lanAction = new QAction(this);
QString name ="action_" + currentList.at(i)->Id->toString();
lanAction->setObjectName(name);
lanAction->setText(lanKey);
lanAction->setCheckable(true);
lanAction->setData(i);
connect(lanAction, SIGNAL(triggered(bool)), this, SLOT(ShowSomething(bool)));
ui->menuMy->addAction(lanAction);
}
Here, lanKey is language that may be the same for different actions. Anyway click on the specific action should lead only to checking of this action. What's wrong?
The slot is here:
void VMainWindow::ShowSomething(bool IsTriggered)
{
QAction* senderAction = (QAction*)sender();
int listIndex = senderAction->data().toInt();
if (IsTriggered)
{
CreateEditor(subtitles, listIndex);
}
else
{
//hide this editor
QString name = "editor" + editorsList->Id->toString();
QDockWidget* editorDock = this->findChild<QDockWidget*>(name);
if (editorDock != 0)
{
this->removeDockWidget(editorDock);
this->setLayout(layout());
}
}
}
Thanks
The source of problem is found: it turned out that the slot finds the checked action wrong - by text, not by id.
I can't find a logical issue in the code you posted so far. Here are a couple of options which I would try in order to resolve this problem:
Limit the users possibilities when adding items to a menu so that he can't add two items with the same name.
Add qDebug() output to ShowSomething to see if there is a problem with signals&slots. For example, if the slot gets called once for the first item but twice for the second item there is a problem there.
Debug into CreateEditor step-by-step.
As the problem seems to appear only for actions with a similar name, you should make sure that you never make a lookup of an action (or something related) by its text() but rather by its data() or objectName() (assuming that currentList.at(i)->Id will always be unique)
I've created an ASP.Net user control that will get placed more than once inside of web page. In this control I've defined a javascript object such as:
function MyObject( options )
{
this.x = options.x;
}
MyObject.prototype.someFunction=function someFunctionF()
{
return this.x + 1;
}
In the code behind I've created MyObject in a startup script --
var opts = { x: 99 };
var myObject = new MyObject( opts );
When a certain button in the control is pressed it will call myObject.someFunction(). Now lets say the value of x will be 99 for one control but 98 for another control. The problem here is that the var myObject will be repeated and only the last instance will matter. Surely there's a way to make the var myObject unique using some concept I've haven't run across yet. Ideas?
Thanks,
Craig
Your Javascript like this:-
function MyObject(options) { this.x = options.x; }
MyObject.prototype.someFunction = function() { return this.x + 1; }
MyObject.create(id, options) {
if (!this._instances) this._instances = {};
return this._instances[id] = new MyObject(options);
}
MyObject.getInstance(id) { return this._instances[id]; }
Your startup javascript like this:-
MyObject.create(ClientID, {x: 99});
Other code that needs to use an instance (say in the client-side onclick event)
String.Format("onclick=\"MyObject.getInstance('{0}').someFunction()\", ClientID);
Note the low impact on the clients global namespace, only the MyObject identifier is added to the global namespace, regardless of how many instances of your control are added to the page.
If it is just one value, why not have the function take it as a parameter and build your onclick handler so that it puts the correct value in for each control. If it is more complex than that, then consider making options an array and, for each control, insert the correct options into the spot in the array that corresponds to each particular control. Then pass the proper index into the array into the function.
I do this by using ScriptManager.RegisterClientScriptBlock to register a string as a JavaScript block on the client side. I can then modify my script string using {0}, {1}..,{n} place holders to inject necessary ids. It depends on the structure of your code as to if this is the most elegant fashion, but it works in a pinch. You could then inject variable names using references to Me.ClientID.
You can make the value of "x" static and access it anywhere in the code, such as:
function MyObject( options ) { MyObject.x = options.x; }
MyObject.x = 99; // static
MyObject.prototype.someFunction = function () { return MyObject.x + 1; }
This way you can access MyObject.x anywhere in your code, even without re-instanciating MyObject.
Excellent solution Anthony. The other solutions offered were as good and I did consider them but I was looking for something a little more elegant like this solution.
Thanks!