I have DLL which attaches to non-QT application. In DLL I create QThread and QApplication instances and run event loop. And I have QDialog and QTimer for hiding dialog after 4 sec.
Also I have overridden mousePressEvent but it's didn't work properly. Overridden function is called only when I resize dialog. If I don't do resizing, dialog is hiding and mousePressEvent function didn't call.
Thread:
void MThread::run()
{
MThread::_app = new QApplication(_argc, _argv);
_main_window = new SongTitle();
_main_window->SetTitle(QString::fromLocal8Bit("lalala"));
_main_window->ShowTitle();
MThread::_app->exec();
}
Mouse event:
void SongTitle::mouseReleaseEvent(QMouseEvent *event)
{
MessageBoxW(0, L"in mouse event",L"",0);
switch (event->button()){
case Qt::LeftButton:
MessageBoxW(0, L"left",L"",0);
break;
case Qt::RightButton:
MessageBoxW(0, L"right",L"",0);
break;
default:
QDialog::mousePressEvent(event);
break;
}
}
How to fix this behavior?
You seem to be overriding the mouse release event, not the press event. Also there's no reason to use windows APIs like MessageBox when you've got Qt equivalents like QMessageBox.
Most likely you miss the fact that a widget will only get the events that haven't been used up by other widgets that are on top of it. Your SongTitle dialog likely has child widgets, and you are clicking on those widgets, not on SongTitle itself.
If you want to get mouse events as they come to all of widget's children, you can install an event filter on all child widgets. That event filter will receive all events as they come in.
Related
In WPF, there is a Closing event on <Window... tag, where we can hook up some dispose code in MainWindow_OnClosing.
But there is no such event in UWP app. The closing I guess is Unloaded, not even Unloading is there.
I just placed my disposing code there but still feel concerned. Is Unloaded event supposed for this type of work? Is there something I need to take note?
According to the MSDN, the Window class has a Closed event.
I'm mentioning this as you posted the Closing event of a window component, but keep in mind the remark of the event:
The Closed event occurs when a Window closes. However, Windows Store
apps typically use a single Window instance, and do not open and close
additional Window instances.
Now, when using the Frame navigation system of the main window with Pages, I advise you to use the OnNavigatedTo and OnNavigatedFrom events to manipulate all initialisation and dispose functionality of the class.
You may want to pay attention to the OnNavigationFrom as it is invoked immediately before the Page is unloaded and is no longer the current source of a parent Frame.
A really simple example:
Windows.ApplicationModel.Resources.ResourceLoader loader;
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
loader = new Windows.ApplicationModel.Resources.ResourceLoader();
var navigationPageContentFormat = loader.GetString("NavigationPageContent");
var navigationPageContentWhenEmpty = loader.GetString("NavigationPageContentWhenEmpty");
this.ParameterTextBlock.Text = String.Format(navigationPageContentFormat, e.Parameter?.ToString() ?? navigationPageContentWhenEmpty);
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
loader = null;
}
Nowadays, I am working on a java swing application using DJ NativeSwing as my embed browser to do something automatic work. The scenario of my application is that a user click start button and the embed browser auto click some position of the current page and then redirect to another one and then execute some other operations like click or something other. Now here is my solution. First, I will define a webbrowser class (extends JWebBrowser) and webbrowser listener class (implements WebBrowserListener), which represents a webbrowser and contains loadingProgressChanged, windowOpening and so on separately. Second, I define a thread class to do some logic computing and execute my browser operations as mentioned above by webbrowser.executeJavascript. At last, I add mouseListener for start and stop button to start or stop the task. When I open my application, the JFrame add the browser and its listener class. I click the start button, the browser works and will click the target position as I expected and then the page will redirect to another one. As we all know, js code can’t be executed until the page was loaded completely. So I set a global flag to check whether the page has loaded completely or not in loadingProgressChanged (code:if(e.getWebBrowser().getLoadingProgress() == 100)globalflag = true;) within webbrowser listener class. And in the thread class, I use code( while(globalflag == false){Thread.sleep(500);}) after first click to detect if the current page was loaded completely. However, when browser first click the target position and the page redirects successfully, I find that the current page has changed but blocked. After some debugging, I find it. In my thread class, browser will execute js code by webbrowser.executeJavascript("document.getElementById(‘target’).click();") to click the target position and then java code (while(globalflag == false){Thread.sleep(500);}) to detect if the current page was loaded completely and then execute some other java code. I find that the globalflag will never change and the current page’s loadingProgressChanged listener will never work because the java code (while(globalflag == false)). Because after I remove all java code after the first webbrowser.executeJavascript("document.getElementById(‘target’).click();"), the current page’s loadingProgressChanged listener works and the page was not blocked. With the DJ NativeSwing demo, I could execute my js in the loadingProgressChanged. However, I want to do a series of operations with my browser and also want to stop the task whenever need. So, I prefer to my solution for my demand rather than the provided one by demo. So, I wonder that after webbrowser.executeJavascript the DJ NativeSwing thread will wait my thread? And, in my software architecture, does anyone could have any suggestions? Thanks a lot. Any suggestion is appreciated!
PS.my application works fine with jdic-0.9.5, but it supports IE7 only.
I paste my code here to demonstrate my problem:
After I click the start button in JFrame, I will new a thread as follow
public class MyVisit implements Runnable{
private void doClick1(){
webbrowser.executeJavascript("document.getElementById('n').value='test'");
webbrowser.executeJavascript("document.getElementById('f').submit()");
}
public void run() {
globalFlag=false;
webbrowser.executeJavascript("document.getElementById(‘t’).click();") ;
while(!globalFlag){
Thread.sleep(500);
}
this.doClick1();
}
}
listener:
public class MyListener implements WebBrowserListener {
…
public void loadingProgressChanged(WebBrowserEvent e) {
if (e.getWebBrowser().getLoadingProgress() == 100) {
globalFlag=true;
}
}
}
DJ Native Swing enforces the Swing approach to threading: manipulate the components in the AWT Event Dispatch thread (a.k.a. UI thread). But also, do not block that thread, because listeners are triggered in that thread too.
So, you should not have a "while" loop in the UI thread, but should instead spawn a new thread. I hope your global variable is volatile, or AtomicBoolean or all accesses to it protected by synchronized block, otherwise you might have interesting threading side effects.
I'm trying to do a simple chat application on javafx
my actual problem is the thread to insert updates into a observablelist and set it on a listview
the code im using :
String message_reçu;
try {
out = new PrintWriter(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
while (true) {
message_reçu = in.readLine();
if (message_reçu != null) {
messagesReçus.add(message_reçu);
}
if (message_reçu.equals("QUIT")) {
break;
}
}
in.close();
out.close();
socket.close();
I did this inside of a runnable class and once the server fire a msg the thread insert the msg on the list and shows on the listview but the thread dies instead of keep the work
I did a search on it and every one says to use a runlater but I’m completely lost there I did declare a runlater but I’m not sure how to execute it so any help is welcome
Thanks
Other Answers
This question is largely a duplicate of the following questions, so also refer to the answers to those:
Usage of JavaFX Platform.runLater and access to UI from a different thread
Platform.runLater and Task in JavaFX
Solution
Your question is little more specific than those though, so I'll provide some extra info.
For your particular code you want to wrap the add call in Platform.runLater:
Platform.runLater(new Runnable() {
public void run() {
messagesReçus.add(message_reçu);
}
});
Everything else in your example stays as it is.
Background Information
JavaFX UI updates must be made on the JavaFX application thread or there is a high likelihood that your program will malfunction in unexpected ways.
The ListView control listens for changes to the ObservableList backing the ListView cell values. When that list changes, a UI update is immediately triggered on the thread the originally updated the list.
Once you wrap the list modifications in Platform.runLater, you ensure that the subsequently triggered UI update is performed on the JavaFX application thread rather than your user thread.
i have added following coe in my view class but still it's not working.
BOOL CtestView::PreCreateWindow(CREATESTRUCT& cs)
{
if (CView::PreCreateWindow(cs))
{
WNDCLASS wc;
// Get the class information for the default MFC view class.
::GetClassInfo(AfxGetInstanceHandle(),cs.lpszClass,&wc);
// Register a new class using the attributes of the default
// view class, and make it the new default class.
cs.lpszClass = AfxRegisterWndClass(wc.style | CS_DBLCLKS,
wc.hCursor,wc.hbrBackground,wc.hIcon);
return TRUE;
}
return FALSE;
}
Is your window set to receive double click events? See this MSDN article. Quote:
An application-defined window does
not, by default, receive double-click
messages. Because of the system
overhead involved in generating
double-click messages, these messages
are generated only for windows
belonging to classes that have the
CS_DBLCLKS class style. Your
application must set this style when
registering the window class.
You can also create your own double-clicks by managing up/down events, the delta between the cursor locations of those events and the time between them.
So for example, this sequence:
Down message (save control)
start timer
less than 250 milliseconds passed
up message (on same control)
less than 250 milliseconds
Down message (on same control)
less than 250 milliseconds
up message (on same control)
= double click
If in that sequence your timeout expires or the control changes you reset your counters and variables since the action was not a double click.
How can I stop mfc dialog application closing by pressing ESC (Escape key).
After executing my application if I press ESC key then the window is closed.
How can this be stopped?
I am using VC++ 6.0.
You can override the OnCancel event and only move forward with the OnCancel call if IDCANCEL is the focused item.
void CMyDialog::OnCancel(void)
{
if(GetDlgItem(IDCANCEL) == GetFocus())
{
CDialog::OnCancel();
return;
}
}
There are different ways to do this. You can:
Create an OnCancel Handler and do whatever you want with the Cancel notification
You can Handle OnClose Event and do whatever you want.
You can override PreTranslateMessage and check Esc key there and do whatever you want.
Check this for code examples.
For a PreTranslateMessage example, see this
Override OnCancel and don't call the base class implementation.
Don't go near OnClose unless you know what you're doing, you risk breaking the behaviour for Alt-F4 and the X button.
I've always regarded PreTranslateMessage for things like this as using a thermo-nuclear weapon to crack a nut, but if it floats your boat...
Assuming we're dealing with a top-level window implemented as a CDialog subclass here, that window can receive two "kinds" of close events:
Application close events (WM_SYSCOMMAND with an ID of SC_CLOSE)
Window close events (WM_COMMAND with an ID of IDOK or IDCANCEL)
MFC, however, effectively routes the former class of events through CDialog::OnCancel by default when they are sent to a dialog, which means that overriding OnCancel also breaks Alt-F4 and the X button. This means that in order to distinguish between the two, you need to handle the former events in OnSysCommand while using overrides of OnOK and OnCancel to handle the latter.
The resulting code looks something like this:
class CTopLevelDlg: public CDialog
{
afx_win void OnSysCommand(UINT id, LPARAM lparam) override
{
if (id == SC_CLOSE)
CDialog::OnCancel();
}
void OnOK() override {}
void OnCancel() override {}
};