Qt 保存设置参数与错误信息

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_43383722/article/details/87007278

前言

Qt 串口通信程序有数个设置参数,包括串口、波特率、数据位等,所以希望在设置合适的参数并保存后,程序再次打开时能够自动加载这些参数。此外该程序的另一个任务是保存通信过程中的错误信息。

Qt 读取/保存设置参数

待续。

Qt 保存错误信息

学习记录-Qt将调试信息保存到文件
博客中简要介绍了将调试信息打印到文件的方法。注册 qInstallMsgHandler() 后所有的打印信息都会保存到指定的文件中。博客中实现保存调试信息的代码如下:

 #include <qapplication.h>
 #include <stdio.h>
 #include <stdlib.h>
 
 void myMessageOutput(QtMsgType type, const char *msg)
 {
    QString txt;
    QString tim = QDate::currentDate().toString("yyyyMMdd") + QTime::currentTime().toString("hhmmss") + QString("-");
    switch (type) {
    case QtDebugMsg:  //调试信息提示
        txt = tim + QString("Debug: %1\n").arg(msg);
        break;
    case QtWarningMsg: //一般的warning提示
        txt = tim + QString("Warning: %1").arg(msg);
        break;
    case QtCriticalMsg: //严重错误提示
        txt = tim + QString("Critical: %1").arg(msg);
        break;
    case QtFatalMsg: //致命错误提示
        txt = tim + QString("Fatal: %1").arg(msg);
        //abort();
    }
 
    QFile outFile("logFile.txt"); //写入文件
    outFile.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream ts(&outFile);
    ts << txt << "\r\n" << endl;
    if(QtFatalMsg == type){
        abort();
    }
 }
 
 int main(int argc, char **argv)
 {
     qInstallMsgHandler(myMessageOutput);
     QApplication app(argc, argv);
     ...
     return app.exec();
 }

展开阅读全文

SharedPreferences 保存用户设置参数

11-19

不知道这样写,有什么问题,总之是没有保存设置的参数。参数设置后,退出来再进去,还是默认的设置。rn[code=java]rnimport java.util.ArrayList;rnimport java.util.HashMap;rnimport java.util.List;rnimport java.util.Map;rnimport java.util.Timer;rnimport java.util.TimerTask;rnrnimport android.annotation.TargetApi;rnimport android.app.Activity;rnimport android.app.AlertDialog;rnimport android.app.Dialog;rnimport android.content.Context;rnimport android.content.DialogInterface;rnimport android.content.SharedPreferences;rnimport android.os.Bundle;rnimport android.view.KeyEvent;rnimport android.view.LayoutInflater;rnimport android.view.View;rnimport android.view.View.OnKeyListener;rnimport android.view.WindowManager;rnimport android.view.inputmethod.InputMethodManager;rnimport android.widget.AdapterView;rnimport android.widget.AdapterView.OnItemClickListener;rnimport android.widget.ArrayAdapter;rnimport android.widget.BaseAdapter;rnimport android.widget.EditText;rnimport android.widget.ImageView;rnimport android.widget.ListView;rnimport android.widget.Spinner;rnimport android.widget.TextView;rnrnpublic class MainActivity extends Activity rn private ListView mListView;rn private List> data;rn private ArrayAdapter adapter;rn private ArrayAdapter adapter1;rn private String[] m;rn private String[] arrMenuItemName;rn private String[] arrNumber;rn CustomListAdapter listAdapter;rnrn private final String PREFERENCES_NAME="survey";rn public static final String POS = "NAME"; rn public static final String FIRST_RUN = "first";rn boolean first;rn rn @Overridern public void onCreate(Bundle savedInstanceStated) rn super.onCreate(savedInstanceStated);rn rn setContentView(R.layout.list_view);rn m = this.getResources().getStringArray(R.array.switcher);rn rn arrMenuItemName= this.getResources().getStringArray(R.array.menu_array);rn rn arrNumber = this.getResources().getStringArray(R.array.number_array);rn rn adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, m);rn rn adapter1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, arrNumber);rn rn mListView = (ListView) findViewById(R.id.list_view_id);rn rn data = getData();rn listAdapter = new CustomListAdapter();rn mListView.setAdapter(listAdapter);rn rn rn mListView.setOnKeyListener(new OnKeyListener() rnrn public boolean onKey(View arg0, int arg1, KeyEvent arg2) rn // TODO Auto-generated method stubrn rn return false;rn rn rn );rn rn rn public boolean onKeyDown(int keyCode, KeyEvent event)rn// System.out.println("FFFFFFFFFF "+ mListView.getSelectedItemPosition());rn int pos = mListView.getSelectedItemPosition();rnrn if (pos >= 0) rn int i = 0;rn if (event.getAction() == KeyEvent.ACTION_DOWN) rn switch (keyCode) rn case KeyEvent.KEYCODE_DPAD_LEFT:rn i = (Integer) data.get(pos).get("sss");rn i = i - 1;rn if (i < 0) rn rn switch(pos)rn case 0:rn i = m.length - 1;rn break;rn case 1:rn i = m.length - 1;rn break;rn case 2:rn i = arrNumber.length - 1;rn break;rn rn rn rn rn data.get(pos).put("sss", i);rn listAdapter.notifyDataSetChanged();rn return true;rnrn case KeyEvent.KEYCODE_DPAD_RIGHT:rn i = (Integer) data.get(pos).get("sss");rn i = i + 1;rn switch(pos)rn rn case 0:rn if (i >= m.length) i = 0;rn break;rn case 1:rn if (i >= m.length) i = 0;rn break;rn case 2:rn if (i >= arrNumber.length) i = 0;rn break;rn rn rn data.get(pos).put("sss", i);rn listAdapter.notifyDataSetChanged();rn return true;rn rn rn rn return super.onKeyDown(keyCode, event);rn rn private List> getData() rn List> list = new ArrayList>();rn Map map;rn for(int i = 0; i< arrMenuItemName.length;i++)rn map = new HashMap();rn map.put("menuText", arrMenuItemName[i]);rn map.put("leftOfImage", R.drawable.gray_l);rn map.put("rightOfImage", R.drawable.gray_r);rn map.put("sss", 0);rn list.add(map);rn rn rn return list;rn rnrn class CustomListAdapter extends BaseAdapter rnrn public Object getItem(int arg0) rn // TODO Auto-generated method stubrn return arg0;rn rnrn public long getItemId(int position) rn // TODO Auto-generated method stubrn return position;rn rnrn public int getCount() rn // TODO Auto-generated method stubrn return data.size();rn// return 3;rn rnrn @TargetApi(11)rn public View getView(int position, View convertView, android.view.ViewGroup parent) rn final ViewHolder viewHolder;rn int widgetPosition = 0;rn int flag = 0;rn rn if (convertView == null) rn viewHolder = new ViewHolder();rn// convertView = mInflater.inflate(R.layout.item_custom, null);rn convertView = getLayoutInflater().inflate(R.layout.item_custom, null);rn viewHolder.leftOfImage = (ImageView) convertView.findViewById(R.id.menu_image_id);rn viewHolder.rightOfImage = (ImageView) convertView.findViewById(R.id.right_image_id);rn viewHolder.menuText = (TextView) convertView.findViewById(R.id.menu_text_id);rn viewHolder.mSpinner = (Spinner) convertView.findViewById(R.id.menu_spinner_id);rnrn SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);rn rn first = preferences.getBoolean(FIRST_RUN, true);rn// System.out.println("i--->itemPosition "+itemPosition);rn if(first)rn flag = 1;rn widgetPosition = preferences.getInt(POS, 1); rn rn convertView.setTag(viewHolder);rn else rn viewHolder = (ViewHolder) convertView.getTag();rn rn rn if(flag == 1)rn flag = 0;rn rn elsern widgetPosition =(Integer) data.get(position).get("sss");rn rn rn viewHolder.menuText.setText((String) data.get(position).get("menuText"));rn viewHolder.leftOfImage.setBackgroundResource((Integer) data.get(position).get("leftOfImage"));rn viewHolder.rightOfImage.setBackgroundResource((Integer) data.get(position).get("rightOfImage"));rn rn switch(position)rn case 0:rn viewHolder.mSpinner.setAdapter(adapter);rn viewHolder.mSpinner.setSelection(widgetPosition);rn break;rn case 1:rn viewHolder.mSpinner.setAdapter(adapter);rn viewHolder.mSpinner.setSelection(widgetPosition);rn break;rn case 2:rn viewHolder.mSpinner.setAdapter(adapter1);rn viewHolder.mSpinner.setSelection(widgetPosition);rn break;rn rn rn return convertView;rn rn rn protected void onStop() rn //获得SharedPreferences对象rn SharedPreferences MyPreferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);rn //获得SharedPreferences.Editorrn SharedPreferences.Editor editor = MyPreferences.edit();rn //保存组件中的值rn editor.putInt(POS, 0);rn editor.putBoolean(FIRST_RUN, false);rn //提交保存的结果rn editor.commit();rn rn rn public class ViewHolder rn public TextView menuText;rn public ImageView leftOfImage;rn public Spinner mSpinner;rn public ImageView rightOfImage;rn rn rn rnrn[/code] 论坛

没有更多推荐了,返回首页