Problems creating progress bar in Visual Studio using MFC - mfc

i'm trying to simply fill a progress bar, full when a check box is checked, and empty when the box is unchecked. there is an ONCLICK action for the check box, so i figured i would check the value every time it was clicked, and would fill the bar only when it was checked.
this code includes a couple different things i tried, anything with progCtrl gave me a runtime error. any thoughts would be helpful, thanks!
void Cgui1Dlg::OnBnClickedsetkill()
{
// TODO: Add your control notification handler code here
//IDC_PROGRESS.Value = 100;
//CProgressCtrl progCtrl;
//progCtrl.SetDlgCtrlID(IDC_PROGRESS);
//UpdateData();
//if(changefill)
//{
//IDC_PROGRESS.PBM_SETPOS(100);
//SendMessage(IDC_PROGRESS, PBM_SETPOS, 100);
//progCtrl.SetPos(100);
//}
//else
//{
//filled = FALSE;
//}
UpdateData(FALSE);
}

I would create a control variable for the progress control and the check button. Then, do:
void Cgui1Dlg::OnBnClickedsetkill()
{
if(c_Check.GetCheck()==BST_CHECKED)
{
c_Progress.SetPos(100);
}
else
{
c_Progress.SetPos(0);
}
}

Related

Qt message box - show message box until timeout

I have to show the saving message box until timeout.
Once the timeout happens go to slot and do some function.
timerToSave=new QTimer(this);
connect(timerToSave,SIGNAL(timeout()),this,SLOT(SavingStatusSlot()));
Above code is the timer, when timeout moves to the saveslot.
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(SetThread::getInstance()->UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(0);
msgBox->setModal(true);
msgBox->setText("Saving ... ");
msgBox->setIcon(QMessageBox::Information);
msgBox->setStandardButtons(QMessageBox::Ok);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setWindowFlags(Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->exec();
}
else
SavingStatusSlot();
return saveStatus;
}
Above method called from other classes, when the user click on the save button.
once the method called, starting the timer then displaying the message box.
if timeout happens calling slot [given below]
void PopUpManager::SavingStatusSlot()
{
msgBox->button(QMessageBox::Ok)->animateClick();
timerToSave->stop();
if(SetThread::getInstance()->UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" Saving Error ");
saveStatus=false;
}
}
this code is working, I have used the message box with OK button and when the timeout creating the animated click and doing some function.
Now I want to show the Message box without button and when timeout, close the message box then do some function
But the Message box close() is not working.
void PopUpManager::ClosePopUP()
{
if(msgBox->isEnabled())
msgBox->close();
}
if I call the above code the message box has to close, but it is displaying.
Can anyone help me on this.
Thanks in advance.
I have solved the issue
used msgBox->show(); instead of the msgBox->exec();
and msgBox->hide(); insted of msgBox->close();
code is given below.
bool PopUpManager::PopUpSaveStaus()
{
timerToSave->start(3000);
saveStatus=false;
if(UISaveStatus==ST_PROCESSING)
{
msgBox = new QMessageBox(QMessageBox::Information,"Error","Processing ... ",0,0,Qt::FramelessWindowHint| Qt::WindowStaysOnTopHint);
msgBox->setStandardButtons(0);
msgBox->setCursor(Qt::WaitCursor);
msgBox->setStyleSheet("background-color:#444;color:#FFF;outline:none;");
msgBox->show();
}
else
{
SavingStatusSlot();
}
return saveStatus;
}
void PopUpManager::SavingStatusSlot()
{
msgBox->hide();
timerToSave->stop();
if(UISaveStatus==ST_OK)
{
saveStatus=true;
}
else
{
PopUpWithOKButton(" communication Failed ");
saveStatus=false;
}
}

Canceling an accepted dialog in Qt (aka user error checking)

I feel like I'm missing something obvious, but I want to be able to error check what the user inputted in a modal dialog when they hit 'OK', and allow them to go back and fix it without closing the dialog.
Is there something I can set within the 'OK' button's slot callback that will tell the dialog not to close?
You can use done method which you could know return value is rejected or not.
for example:
void ExDialog::done(int res)
{
if (res == QDialog::Accepted)
{
// check if it is ok or not
if(not)
{
ShowErrPopUp();
return;
}
}
QDialog::done(res);
}
// when ok button is clicked
void ExDialog::action_ok_bt_clicked()
{
this->accept();
}

Why double click event detecting on empty area of listBox in mfc

case 1 : I have a MFC dialog box having a LisBox.
I have added two items in listbox.
Whenever i am double clicking on empty area of list box i.e. not double clicking
on either of two item.
Double click is detecting on empty area of listbox.
case 2: When i created a small MFC test application with listbox. it iis detecting double click only on item, not on empty area.
I compared all properties of both cases but couldn't figure out what is the problem.
Anyone has idea what is going wrong in case 1.
I think it is abnormal process. I've tested your situation in VS2010. In my MFC test application sent LBN_DBLCLK when I double clicked on empty area. If you do not really want to know the reason this weired situation, you can just check whether double click event is occurred on empty area or not. I think it is better way for saving your time.
void CMfcDlgTestDlg::OnLbnDblclkList2()
{
// TODO: Add your control notification handler code here
CListBox* list = (CListBox*)(GetDlgItem(IDC_LIST2));
int cur_sel = list->GetCurSel();
if (cur_sel == -1)
{
return;
}
}
EDIT : FOR ANOTHER CASE
When one of list box item is already selected, how can it handle on ON_LBN_DBLCLK handler?
I think there will be some available methods for solving this, however I use below code and it can be useful way, also.
void CMfcDlgTestDlg::OnLbnDblclkList2()
{
// TODO: Add your control notification handler code here
CListBox* list = (CListBox*)(GetDlgItem(IDC_LIST2));
CPoint cursor;
cursor.x = GetCurrentMessage()->pt.x;
cursor.y = GetCurrentMessage()->pt.y;
list->ScreenToClient(&cursor);
BOOL is_outside = FALSE;
UINT item_index = list->ItemFromPoint(cursor, is_outside);
if(is_outside)
{
//mouse clicked on empty area
return ;
}
else
{
// do something with 'item_index'
}
}
I hope this will help you a little.

GetCheck() working for check buttons but not radio buttons?

I'm just learning about making dialog boxes with MFC in Visual Studio 2010.
From what I can see on msdn's website, the GetCheck() function should work the same for both check boxes and radio buttons.
Basically I have two radio buttons next to two sliders. If one radio button is on, that slider is disabled, and if the other radio button is on, then the other slider is disabled.
When I had the radio buttons switched out for check boxes, this was the code i used:
void Cstring_copyierDlg::OnBnClickedSld1chk()
{
UINT nCheck = m_slide1check.GetCheck();
if(nCheck == BST_CHECKED){
m_slider.EnableWindow(FALSE);
}
else{
m_slider.EnableWindow(TRUE);
}
}
void Cstring_copyierDlg::OnBnClickedSld2chk()
{
UINT nCheck = m_slide2check.GetCheck();
if(nCheck == BST_CHECKED){
m_slider2.EnableWindow(FALSE);
}
else{
m_slider2.EnableWindow(TRUE);
}
}
This code worked fine. But when I switched out the check boxes for radio buttons, this is the code i used:
void Cstring_copyierDlg::OnBnClickedRad1()
{
UINT nCheck = m_radio1.GetCheck();
if(nCheck == BST_CHECKED){
m_slider.EnableWindow(FALSE);
}
else{
m_slider.EnableWindow(TRUE);
}
}
void Cstring_copyierDlg::OnBnClickedRad2()
{
UINT nCheck = m_radio2.GetCheck();
if(nCheck == BST_CHECKED){
m_slider2.EnableWindow(FALSE);
}
else{
m_slider2.EnableWindow(TRUE);
}
}
However the second piece of code returns the following error upon compilation:
error C2228: left of '.GetCheck' must have class/struct/union
What gives?
You are choosing the wrong variable type for the DDX in the add variable wizard. You should choose the control type there.
To change the variable type manually, replace the type of the m_radio1 to CButton and replace DDX_Radio with DDX_Control in DoDataExchange.

How does MFC's "Update Command UI" system work?

I'd like to know more about how this system works, specifically when and how the framework actually decides to update a UI element.
My application has a 'tools' system where a single tool can be active at a time. I used the "ON_UPDATE_COMMAND_UI" message to 'check' the tool's icon/button in the UI, which affected both the application menu and the toolbars. Anyway, this was all working great until some point in the last couple of days, when the toolbar icons stopped getting highlighted properly.
I investigated a little and found that the update command was only being received when the icon was actually clicked. What's strange is this is only affecting the toolbars, not the menu, which is still working fine. Even when the buttons in the menu are updated the toolbar icon stays the same.
Obviously I've done something to break it - any ideas?
EDIT:
Never mind. I'd overwritten the Application's OnIdle() method and hadn't called the original base class method - that is, CWinApp::OnIdle() - which I guess is where the update gets called most of the time. This code snippet from https://msdn.microsoft.com/en-us/library/3e077sxt.aspx illustrates:
BOOL CMyApp::OnIdle(LONG lCount)
{
// CWinApp's original method is involved in the update message handling!
// Removing this call will break things
BOOL bMore = CWinApp::OnIdle(lCount);
if (lCount == 0)
{
TRACE(_T("App idle for short period of time\n"));
bMore = TRUE;
}
// ... do work
return bMore;
// return TRUE as long as there are any more idle tasks
}
Here's a good article that kinda explains how to do it. Don't use his code example with WM_KICKIDLE though, instead scroll down to the comments section. There are two code samples that explain how to do it better. I quote:
//Override WM_INITMENUPOPUP
void CDialog::OnInitMenuPopup(CMenu* pPopupMenu, UINT nIndex, BOOL bSysMenu)
{
CDialog::OnInitMenuPopup(pPopupMenu, nIndex, bSysMenu);
// TODO: Add your message handler code here
if(pPopupMenu &&
!bSysMenu)
{
CCmdUI CmdUI;
CmdUI.m_nIndexMax = pPopupMenu->GetMenuItemCount();
for(UINT i = 0; i < CmdUI.m_nIndexMax; i++)
{
CmdUI.m_nIndex = i;
CmdUI.m_nID = pPopupMenu->GetMenuItemID(i);
CmdUI.m_pMenu = pPopupMenu;
// There are two options:
// Option 1. All handlers are in dialog
CmdUI.DoUpdate(this, FALSE);
// Option 2. There are handlers in dialog and controls
/*
CmdUI.DoUpdate( this, FALSE );
// If dialog handler doesn't change state route update
// request to child controls. The last DoUpdate will
// disable menu item with no handler
if( FALSE == CmdUI.m_bEnableChanged )
CmdUI.DoUpdate( m_pControl_1, FALSE );
...
if( FALSE == CmdUI.m_bEnableChanged )
CmdUI.DoUpdate( m_pControl_Last, TRUE );
*/
}
}
}
See if this helps - http://msdn.microsoft.com/en-us/library/essk9ab2(v=vs.80).aspx