这可能是一个重复的问题,但我找不到它,可能是由于可能缺乏术语。
问题在于:我想将一个lambda函数赋给稍后会调用的变量。
class MyClass
{
public:
MyClass( QWdiget* widget );
void doSomething();
private:
QWidget* my_widget;
std::function<QString()> my_text;
}
MyClass::MyClass( QWdiget* widget )
: my_text( [=](){return QString();} )
{
if ( qobject_cast<QLabel*>( widget ) )
{
my_text = [=](){return my_widget ? qobject_cast<QLabel*>( my_widget )->text() : Qstring(); };
}
else if ( qobject_cast<QGroupBox*>( widget ) )
{
my_text = [=](){return my_widget ? qobject_cast<QGroupBox*>( my_widget )->title() : QString(); };
}
else
{
return;
}
my_widget = widget;
}
MyClass::DoSomething()
{
QString str = my_text();
....whatever
}
我遇到了崩溃但无法找到。
修改
正确实施默认值可部分解决问题。
现在,qobject_cast
崩溃。
答案 0 :(得分:3)
添加如下默认实现:
public class overview extends TabActivity {
private MyApplication app;
private JSONArray v;
private TabWidget m_tabs;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
app = ((MyApplication) getApplicationContext());
app.ActivityMode = true;
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(gvendorservice.msgID);
int pending=0;
int delivered=0;
TabHost tabHost = getTabHost();
// Tab for ShowOrders
TabSpec ShowOrders = tabHost.newTabSpec("Normal Order");
// setting Title and Icon for the Tab
ShowOrders.setIndicator("Normal Order");
Intent ShowOrdersIntent = new Intent(this, Orders.class);
ShowOrders.setContent(ShowOrdersIntent);
// Tab for Showchatlist
TabSpec Showchatlist = tabHost.newTabSpec("Chat");
Intent ShowchatlistIntent = new Intent(this, activity_chatlist.class);
Showchatlist.setIndicator("CHATS\n\t"+app.getNewMsgCount().length());
Showchatlist.setContent(ShowchatlistIntent);
// Tab for AcceptOrder
TabSpec AcceptOrder = tabHost.newTabSpec("Order");
Intent AcceptOrderIntent = new Intent(this, activity_acceptorder.class);
AcceptOrder.setIndicator("OrderS\n\t"+app.getOpenOrders().length());
AcceptOrder.setContent(AcceptOrderIntent);
//Tab for MyChatOrder
TabSpec MyChatOrder = tabHost.newTabSpec("Pending");
Intent MyChatOrdertIntent = new Intent(this, activity_mychatorders.class);
MyChatOrdertIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
JSONArray orderlistArray = app.getMyChatOrders();
try {
TextView tcp = (TextView) findViewById(R.id.txtChatOrdersPending);
for (int i = 0; i < orderlistArray.length(); i++) {
JSONObject cat = orderlistArray.getJSONObject(i);
if (cat.getInt("delivered") == 0) {
pending++;
} else {
delivered++;
}
}
tcp.setText(String.valueOf(pending));
}
catch(JSONException e)
{
}
MyChatOrder.setIndicator("Pending\n\t"+String.valueOf(pending));
MyChatOrder.setContent(MyChatOrdertIntent);
// Adding all TabSpec to TabHost
tabHost.addTab(ShowOrders); // Adding normal order tab
tabHost.addTab(Showchatlist); // Adding chat tab
tabHost.addTab(AcceptOrder); // Adding orders tab
tabHost.addTab(MyChatOrder);//Adding pending tab
for (int i = 0; i < tabHost.getTabWidget().getTabCount(); i++) {
TabWidget tw = (TabWidget) tabHost.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(i);
TextView tv = (TextView) tabView.findViewById(android.R.id.title);
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setAllCaps(true);
}
}
@Override
protected void onResume() {
super.onResume();
app.ActivityMode = true;
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel(gvendorservice.msgID);
int pending=0;
int delivered=0;
JSONArray orderlistArray = app.getMyChatOrders();
try {
TextView tcp = (TextView) findViewById(R.id.txtChatOrdersPending);
for (int i = 0; i < orderlistArray.length(); i++) {
JSONObject cat = orderlistArray.getJSONObject(i);
if (cat.getInt("delivered") == 0) {
pending++;
} else {
delivered++;
}
}
tcp.setText(String.valueOf(pending));
}
catch(JSONException e)
{
}
}
@Override
protected void onDestroy() {
super.onDestroy();
app.ActivityMode = false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_overview, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
Intent intent;
//noinspection SimplifiableIfStatement
if (id == R.id.action_logout) {
app = ((MyApplication) getApplicationContext());
app.logOut();
intent = new Intent(overview.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
overview.this.startActivity(intent);
overview.this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}