Enable button function inside backgroundWorker - button

I am using windows form application to create gui. I have create a form with several button. The functionality of the first button called button1 is to read a video from hard disk and display it to a picturebox. The last line of button1 code is to enable another button:
button2->Enabled = true;
Button1 code is inside a backgroundworker. The result of this, it works fine, however it doesnt enable the button2. Is there issue using button properties inside backgroundworker?

You have to use BeginInvoke method and use Action delegate because backgroundworker DoWork doesn't modify UI.
private:
void DoWork(Object^ /*sender*/, EventArgs^ /*e*/ )
{
// some code
button2->BeginInvoke(gcnew Action(this, &MyForm::ModifyButton) );
}
void ModifyButton()
{
button2->Enabled = true;
}

Related

QT , my form was hidden after calling setWindowFlags() in my slots

I just want to disable form's close button while doing a task(by QThread), So I connected the thread's signal "started()" and "finished()" to my two slots, for controlling my form's close button.
//...
m_pTestThread = new TestThread();
connect(m_pTestThread, SIGNAL(started()), this, SLOT(onThreadStart()));
connect(m_pTestThread, SIGNAL(finished()), this, SLOT(onThreadFinish()));
m_pTestThread->start();
//...
void QTest::onThreadStart()
{
this->setWindowFlags(this->windowFlags() & (~Qt::WindowCloseButtonHint));
}
void QTest::onThreadFinish()
{
this->setWindowFlags(this->windowFlags() | Qt::WindowCloseButtonHint);
}
After the thread started, my form was hidden... that is strange.
So I call show() after setWindowFlags() function to avoid this problem,
but still don't know why this happened...
Is this the expected behaviour? Should I call show() after setWindowFlags()?
Check the documentation for setWindowFlags here:
Note: This function calls setParent() when changing the flags for a
window, causing the widget to be hidden. You must call show() to make
the widget visible again..

Triggering an action based on its custom shortcut

Suppose I have some action to happen. For that I can create a QAction object and connect its triggered() signal to the slot that executes the desired function. Also, I can have a shortcut associated with the action; by changing the shortcut I'll be able to execute the same action with that shortcut.
My problem now is that the "shortcut" I wanna set to the action, contains also a mouse button press (and mouse events cannot be assigned to action shortcuts); say I want Shift+Left mouse button. Maybe this sounds a little bit harsh but bear with me.
What do I need? Well, I have a button, and an action (say "execute a script"). I want the script to execute when Shift+Left click is clicked, and I want this "shortcut" to be customized, i.e. the user should be able to change to shortcut to, say Ctrl+Left click (from some GUI element, e.g. button text), and now Ctrl+Left click should execute the script.
How can I achieve this?
Note: I as a user would expect an action triggered by a mouse button to be position dependent. If so, the following gets a bit simpler.
Qt doesn't have an option to specify such a shortcut.
You can roll your own by reacting to mouse events:
Maybe you have an event handler mousePressEvent(),
or a generic eventFilter(QObject *obj, QEvent *evt),
or utilize QApplication::notify
Whichever, at some place you need to catch a QMouseEvent *mouseEvt.
Choose the widget (or qApp) that is as outmost as needed.
There, compare mouseEvt->button() and mouseEvt->modifiers() to your list of actions and trigger the selected action. When the user chooses another trigger method, adjust your list of actions.
Let's put this to practice:
class MainWindow : public QWidget {
Q_OBJECT
public:
QMap<QPair<Qt::MouseButton, Qt::KeyboardModifiers>, QAction*> mapMouseShortcuts;
QAction *pLaunchScript;
MainWindow() : QWidget() {
mapMouseShortcuts.insert(qMakePair(Qt::LeftButton, Qt::ControlModifier), pLaunchScript);
}
void mousePressEvent(QMouseEvent *me) {
QAction *action = mapMouseShortcuts.value(qMakePair(me->button(), me->modifiers()), Q_NULLPTR);
if(action != Q_NULLPTR) {
action->trigger();
me->accept(); // optional
}
// optional:
if(!me->isAccepted()) {
QWidget::mousePressEvent(me);
}
}
};

Two Buttons in Qt Gui

I've written a program with different functions in Qt. Now I want to make a Gui. For example I have two buttons, button1 and button2. I open the application, i see button1 first. Then I click button1, it executes its function (like "start") and disappears. Then button2 should appear and when I click button2 it executes its function (like "stop") and disappears and button1 shows up again to be clicked to execute start.
My question now is, how to solve this in an easy way?
void gui::on_pushButton_clicked()
{
//execute start, switch to be button2
}
void gui::on_pushButton_2_clicked()
{
//execute stop, switch to be button 1
}
The following seems to be the easiest solution, but a bit cumbersome in case you want to add more buttons. In that case you might want to consider storing them in a list and iterating the list.
void gui::on_pushButton_clicked()
{
//execute start, switch to be button2
ui->pushButton->hide();
ui->pushButton_2->show();
}
void gui::on_pushButton_2_clicked()
{
//execute stop, switch to be button 1
ui->pushButton->show();
ui->pushButton_2->hide();
}
If you further on decide to implement even more logic, you should consider using QStateMachine and setting certain buttons to visible or hidden when entering or exiting certain states.

How to activate another button using click event of certain button in MS Dynamics AX?

I'm creating a new form in Axapta.
How can I make a Show-->Line View to be activated by clicking on New--> Item?
Should I do this by X++ override methods or it is possible not to use code?
I think it can only be done with code. Override your datasource's create() method and then call the button clicked() method. Note that to call directly the button's Auto declaration propery need to be set to Yes.
For instance in a SalesTable form you could overwrite SalesTable.create() method :
void create(boolean append = true)
{
SalesTable newSalesTable;
EditDetailsButton.clicked();
// rest code goes here
}

How to disable delete button in ASP.NET Dynamic Data?

I need to disable delete button GLOBALLY based on some condition?
The following solutions will not work for me:
http://csharpbits.notaclue.net/2009/07/securing-dynamic-data-preview-4-refresh.html
http://csharpbits.notaclue.net/2008/05/dynamicdata-miscellaneous-bits-part-6.html
Again, I do not want to go into every list and detail page and disable it there.
Why not just extend/inherit from button. You could make your own button that "knows" how to check if it should be hidden:
public class MyButton : Button
{
public void HiddenCheck()
{
bool visible = true;
//Check to see if the button should be hidden
this.Visible = visible;
}
}
Then, just use this button instead of the "System.Web.UI.WebControls.Button" button wherever you need the delete button functionality.
-Make that "Enabled." I read the post again, and I guess you aren't trying to "hide" the button, but disable it. The idea is the same though.

Resources