I wrote some codes for an application
and I want to save this Settings Like Hide a lineEdit or etc...
and when reopen program last settings will load and when user edit setting
settings that saved updates
what I must do?
note: I Used Qsettings but settings dose not saved!
if Possible one person Write a Sample Code For me That save current index of a combobox
QSettings settings("Mobtakeran Fanavri KabooK","Kabook Physiothrapy");
Secretary::Secretary(QWidget *parent) :
QWidget(parent),
ui(new Ui::Secretary)
{
ui->setupUi(this);
ui->comboBox->setCurrentIndex(settings.value("comboBox").toInt());
}
Secretary::~Secretary()
{
QCoreApplication::setOrganizationName("Mobtakeran Fanavri KabooK");
QCoreApplication::setOrganizationName("WWW.M4RZB4Ni.IR");
QCoreApplication::setApplicationName("Kabook Physiothrapy");
delete ui;
}
void Secretary::on_comboBox_currentIndexChanged(int index)
{
settings.beginGroup("comboBox");
if(ui->comboBox->currentIndex()==2) {
ui->pushButton_3->setDisabled(true);
} else if(ui->comboBox->currentIndex()==1) {
ui->pushButton_3->hide();
settings.setValue("comboBox",ui->comboBox->currentIndex());
} else if(ui->comboBox->currentIndex()==0) {
if(ui->lineEdit_56->text()==NULL) {
ui->pushButton_8->setDisabled(true);
}
}
settings.endGroup();
}
when you are saving your settings in Secretary::on_comboBox_currentIndexChanged you are calling settings.beginGroup("comboBox") then you set the value settings.setValue("comboBox",ui->comboBox->currentIndex()).
According to the documentation, this will set the value of the settings "comboBox/comboBox", meaning that you should read its value using settings.value("comboBox/comboBox").toInt().
Also please note that you are calling settings.setValue only in the case where currentIndex changes to 2, are you sure you mean to do that? didn't you mean to call it after all your if/else blocks?
Related
I am developing a UWP windows 10 application and I want to have a page that is only shown at the start of launching application first time. It should not be shown when the app is opened second time in a system.
I have searched online about it but couldn't find any thing about it.
I know that my answer can be refined more, but i have done with spending 2 minutes and its working for me. I have added a page loaded event in my StartPage.xaml.cs file and added the following code inside it.
if (localSettings.Values["IsFirstTime"] == null)
{
localSettings.Values["IsFirstTime"] = true;
}
if ((bool)localSettings.Values["IsFirstTime"])
{
localSettings.Values["IsFirstTime"] = false;
this.Frame.Navigate(typeof(MainPage));
}
Make sure you make a localSettings object at class level (of type ApplicationDataContainer). Now inside App.xaml.cs, I have added global variable for local settings as follow.
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Inside app.xaml.cs file come to the if condition showing rootFrame.Content == null and replace every thing inside it with the following code.
object value = localSettings.Values["IsFirstTime"];
if (localSettings.Values["IsFirstTime"] != null)
{
if ((bool)value)
{
rootFrame.Navigate(typeof(StartPage), e.Arguments);
localSettings.Values["IsFirstTime"] = false;
}
else
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
}
else
{
rootFrame.Navigate(typeof(StartPage), e.Arguments);
localSettings.Values["IsFirstTime"] = false;
}
I have tried it by uninstalling my app and run again to see if start page is shown (and it shows up). Second time when i open, MainPage is shown).
In App.xaml.cs look for the OnLaunched handler. There are these lines of code for "switching" pages:
if (rootFrame.Content == null)
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
}
As #Romasz suggested in comments, you can add here additional logic with local (or better - roaming) storage to check whether app is launched for the first time:
var roamingSettings = ApplicationData.Current.RoamingSettings;
if (roamingSettings.Values.ContainsKey("NotFirstTimeLaunch"))
{
rootFrame.Navigate(typeof(MainPage), e.Arguments);
} else
{
roamingSettings.Values["NotFirstTimeLaunch"] = true;
rootFrame.Navigate(typeof(FirstLaunchPage), e.Arguments);
}
I know there are several topics already on stackoverflow, but nothing that actually solves the problem. Here it is:
Because of some inherent problems with Ribbon Designer I decided to build my next Excel AddIn using XML Ribbon.
However, occasionally I need to make changes to the controls in the ribbon based on user selections. For example I need to change the text of a label, and also make some of the controls disabled in some cases. And here's where I hit a brick wall. It looks like there's no way to do it. I tried to put the logic in the onAction callback as follows:
public void LabelAction(IRibbonControl control)
{
LabelControl label = (LabelControl)control;
label.Label = "changed text";
}
But this cast doesn't work because apparently IRibbonControl interface has nothing to do with the RibbonControl class that LabelConrol inherits from.
I was also not able to find any other way to access any of the XML ribbon controls. Is there even a solution to this? Or should I stick to Ribbon Designer?
You need to do this in a routine that sets the item label.
The xml would look like this:
<button id="SkLabelTest1" getLabel="GetLabelTest" onAction="SkLabelTest1"/>
<button id="SkLabelTest2" getLabel="GetLabelTest" onAction="SkLabelTest2"/>
The routine you are interested in is getLabel
I've done a noddy routine to demonstrate this.
First I added a property to ThisAddin.cs for it to read:
public string _labelTest = string.Empty;
public string LabelTest { get { return _labelTest; } set { _labelTest = value; } }
Then in my ribbon handling code I added the getLabel routine:
public string GetLabelTest(Office.IRibbonControl control)
{
switch (control.Id.ToLower())
{
case "sklabeltest2":
if (Globals.ThisAddIn.LabelTest != string.Empty)
return Globals.ThisAddIn.LabelTest;
else
return "Label Test 2";
default:
return "Label Test 1";
}
}
This works by the SkLabelTest1 button changing the text of SkLabelTest2 and then invalidating the control to force the ribbon to reload it:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
Globals.ThisAddIn._ribbon.InvalidateControl("SkLabelTest2");
}
I've tested just in case and it changes the text OK. Hope this helps
I couldn't make a comment because of my reputation. As a comment to Charlie's post, it is a perfect solution but on my side, I had to change one part.
I changed public void SklabelTest1 function to this one below:
public void SkLabelTest1(Office.IRibbonControl control)
{
Globals.ThisAddIn._labelTest = "Changed text";
this.ribbon.InvalidateControl("SkLabelTest2");
}
And also added this in the beginning of my ribbon class.
private Office.IRibbonUI ribbon;
I hope it helps.
Im working on a MVVM Windows phone app that displays weather info.
When the app loads up it opens MainPage.xaml. It makes a call the the service to get weather info and binds that data to the UI. Both Fahrenheit and Celcius info are returned but only one is displayed.
On the setting page, the user can select to view the temp in either Fahrenheit or Celcius.
The user can change this setting at any time and its stored in IsolatedStorageSettings.
The issue Im having is this:
when the user navigates to the Settings page and changes their preference for either Fahrenheit or Celcius, this change is not reflected on the main page.
This issue started me thinking about this in a broader context. I can see this being an issue in ANY MVVM app where the display depends on some setting in IsolatedStorage. Any time any setting in the IsoStore is updated, how does the ViewModels know this? When I navigate back in the NavigationStack from the settings page back to MainPage how can I force a rebind of the page?
The data in my model hasnt changed, only the data that I want to display has changed.
Am I missing something simple here?
Thanks in advance.
Alex
Probably you have code like this:
public double DisplayTemperature
{
get { return (IsCelsium) ? Celsium : Fahrenheit; }
}
And IsCelsium is:
public double IsCelsium
{
get { return (bool)settings["IsCelsium"]; }
set { settings["IsCelsium"] = value; }
}
So you need to add NotifyPropertyChanged event to notify UI to get new values from DisplayTemperature property:
public double IsCelsium
{
get { return (bool)settings["IsCelsium"]; }
set
{
settings["IsCelsium"] = value;
NotifyPropertyChanged("DisplayTemperature");
}
}
Take a look at Caliburn Micro. You could implement something similar or use CM itself. When using CM I don't even think about this stuff, CM makes it so simple.
When your ViewModel inherits from Screen there are life-cycle events that fire that you can override. For example, OnInitialize fires the very first time the ViewModel is Activated and OnActivate fires every time the VM is activated. There's also OnViewAttached and OnViewLoaded.
These methods are the perfect place to put logic to populate or re-populate data.
CM also has some special built in features for allowing one to easily tombstone a single property or an entire object graph into Iso or phone state.
ok, so Ive come up with a solution. Before I get to it, let me provide some background. The app that Im working on uses both MVVM Light and WP7Contrib. That being the case, I am using Funq for DI and the MVVMLight Toolkit. After I posted my initial question, I gave the question a bit more thought. I remembered a video that I watched a while back from MIX2011 called Deep Dive MVVM with Laurent Bugnion
http://channel9.msdn.com/Events/MIX/MIX11/OPN03
In it, he talks about just this problem (view models not living at the same time) on Windows Phone. The part in question starts around the 19 minute mark.
Anyway, after I remembered that and realized that the ViewModel locator is exposed in App.xaml, this became a trivial problem to solve. When the user changes the Fahrenheit/Celcius option on the setting page, I simply get a reference to the MainViewModel via the ViewModelLocator and reset the collection that is bound to the UI thus causing the bindings to update.
public bool AddOrUpdateValue(string Key, Object value)
{
bool valueChanged = false;
// If the key exists
if (settings.Contains(Key))
{
// If the value has changed
if (settings[Key] != value)
{
// Store the new value
settings[Key] = value;
valueChanged = true;
}
}
// Otherwise create the key.
else
{
settings.Add(Key, value);
valueChanged = true;
}
return valueChanged;
}
public bool ImperialSetting
{
get
{
return GetValueOrDefault<bool>(ImperialSettingKeyName, ImperialSettingDefault);
}
set
{
if (AddOrUpdateValue(ImperialSettingKeyName, value))
{
Save();
RaisePropertyChanged("ImperialSettingText");
var vml = new ViewModelLocator();
vml.MainViewModel.Cities = (App.Current as App).Cities;
}
}
}
It was a mistake on my part not to realize that I could get access to the viewModel via the ViewModelLocator. Hopefully this post saves someone else the time I burned on this issue.
I have a pretty standard MVC3 application. I'm trying to store some data that's application-wide (not user wide) in a the cache (in this case, a Theme object/name). When debugging (on the development server that integrates with Visual Studio), if I call SwitchTheme, I see the new theme right away. On IIS7, whatever theme was cached, stays cached; it doesn't update to the new theme.
Edit: Some code:
public static Theme CurrentTheme { get {
Theme currentTheme = HttpContext.Current.Cache[CURRENT_THEME] as Theme;
if (currentTheme == null)
{
string themeName = DEFAULT_THEME;
try
{
WebsiteSetting ws = WebsiteSetting.First(w => w.Key == WebsiteSetting.CURRENT_THEME);
if (ws != null && !string.IsNullOrEmpty(ws.Value))
{
themeName = ws.Value;
}
}
catch (Exception e)
{
// DB not inited, or we're installing, or something broke.
// Don't panic, just use the default.
}
// Sets HttpContext.Current.Cache[CURRENT_THEME] = new themeName)
Theme.SwitchTo(themeName);
currentTheme = HttpContext.Current.Cache[CURRENT_THEME] as Theme;
}
return currentTheme;
} }
public static void SwitchTo(string name)
{
HttpContext.Current.Cache.Insert(CURRENT_THEME, new Theme(name), null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));
// Persist change to the DB.
// But don't do this if we didn't install the application yet.
try
{
WebsiteSetting themeSetting = WebsiteSetting.First(w => w.Key == WebsiteSetting.CURRENT_THEME);
if (themeSetting != null)
{
themeSetting.Value = name;
themeSetting.Save();
}
// No "else"; if it's not there, we're installing, or Health Check will take care of it.
}
catch (Exception e)
{
// DB not inited or install not complete. No worries, mate.
}
}
I'm not sure where the problem is. I am calling the same method and updating the cache; but IIS7 just shows me the old version.
I can disable output caching in IIS, but that's not what I want to do. That seems like a hacky work-around at best.
Without a code sample it's difficult to know what your problem is. In an attempt to provide some assistance, here is how I frequently set the cache in my applications:
public static void SetCache(string key, object value) {
if (value != null) {
HttpRuntime.Cache.Insert(key, value, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(30));
}
}
The HTTP cache is reset only if you do so manually or the app domain (or app pool) resets for whatever reason. Are you sure that's not happening in this case? And generally speaking, any global static variables would also be maintained in memory under the same circumstances.
There are many reasons why an app pool might be reset at any given point, such as a change to a web.config file, etc. I suggest checking that's not happening in your case.
By the way, output caching is a different thing, although it is maintained in memory largely the same way.
Given that this only happens on IIS7 when Output Caching is not disabled, this seems very likely to be an IIS7 bug. Seriously.
Whether it is or not, is irrelevant to the solution. What you need to do is find some manual process of invalidating the cache, such as touching the web.config file.
But beware: doing this will wipe out the cache (as you expect), but also all static variables (as a side-effect). Whether this is another bug or not, I don't know; but in my case, this was sufficient to solve the problem.
I'm doing something similar to this, but wondering if there is an event somewhere that I'm missing
Store DefaultStore
{
get
{
var defaultStore = mOutlookApp_Model.Session.DefaultStore;
if ( defaultStore.StoreID == mDefaultStore.StoreID )
{
// the default store we set at startup is the same as the default store now, so all good
return mDefaultStore;
}
else
{
// the user changed the default store, so restart the addin_app
DefaultStoreChangedRestartAddIn.Fire();
return null;
}
}
}
readonly Store mDefaultStore;
Nope there isn't anything like this in the API, so hand crafting something is the only way.