对于多线程我们已经有了些许认识,现在就对其进一步的学习应用吧。
把它用在android中实现标题的更换(3秒更换一次):
只需要编写Activity实现即可,这里有两种方法,可以根据自己习惯选择使用(个人较喜欢第一种):
(1)Runnable:
public class MainActivity extends Activity { private int count = 1; private Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: updateTitle(); break; default: break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); new Thread(new TitleThread()).start(); } protected void updateTitle() { setTitle("my bolg" + count++); } class TitleThread implements Runnable { @Override public void run() { while (true) { Message message = new Message(); message.what = 1; handler.sendMessage(message); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
(2)Timer:
public class TimerActivity extends Activity { private int count=1; private Handler handler=new Handler(){ public void handleMessage(android.os.Message msg){ switch(msg.what){ case 1: updateTitle(); break; default: break; } } }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timer); Timer timer=new Timer(); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { Message message=new Message(); message.what=1; handler.sendMessage(message); } }, 1000,3000); } protected void updateTitle() { setTitle("hello"+(count++)); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_timer, menu); return true; } }
是不是很简单,你喜欢哪一种呢?快试试吧!
快速创建多个 页表
使用QStackedWidget 添加页, 页上在添加表
SE_Browser = new QStackedWidget(m_SE); SE_Browser->setObjectName(QString::fromUtf8("SE_Browser")); SE_Browser->setGeometry(QRect(0, 65, 641, 401));
for(j = 1;j <= get_page_count+1;j++) { SE_P[order][j] = new QWidget(); SE_Browser->addWidget(SE_P[order][j]); SE_Table[order][j] = new QTableWidget(SE_P[order][j]); SE_Table[order][j]->setGeometry(2,0,620,400); SE_Table[order][j]->setFrameShape(QFrame::NoFrame); //璁剧疆杈规 SE_Table[order][j]->setAutoScroll(false); SE_Table[order][j]->setShowGrid(true); SE_Table[order][j]->setColumnCount(3);// 设为 3 列 SE_Table[order][j]->setMouseTracking(true); SE_Table[order][j]->verticalHeader()->setDefaultSectionSize(48); SE_Table[order][j]->setSelectionMode(QAbstractItemView::SingleSelection); SE_Table[order][j]->setSelectionBehavior(QAbstractItemView::SelectRows); SE_Table[order][j]->setShowGrid(false); SE_Table[order][j]->setFont(TableFont_Normal); SE_Table[order][j]->setColumnWidth(0,75);// 设置列宽 SE_Table[order][j]->setColumnWidth(1,470);// 设置列宽 SE_Table[order][j]->setColumnWidth(2,80);// 设置列宽 SE_Table[order][j]->setFont(TableFont_Normal); SE_Table[order][j]->verticalHeader()->setVisible(false);// 隐藏 表头 SE_Table[order][j]->horizontalHeader()->setVisible(false);//隐藏 表头 SE_Table[order][j]->setEditTriggers ( QAbstractItemView::NoEditTriggers ); connect(SE_Table[order][j],SIGNAL(cellClicked(int,int)),this,SLOT(SE_clicked(int,int))); }
快速刷新所有空表内容
for(i = 0;i< (count - SE_page_count*8);i++){ for(j = 0;j < 3;j++){ comp_one = show_Comp_info[SE_page_count*8+i+1].one;// 所刷新的 内容数据链表 comp_two = show_Comp_info[SE_page_count*8+i+1].two;// 所刷新的 内容数据链表 comp_three = show_Comp_info[SE_page_count*8+i+1].three;// 所刷新的 内容数据链表 if(j==0) SE_Table[order][SE_page_count+1]->setItem(i,j,new QTableWidgetItem(comp_one)); if(j==1) SE_Table[order][SE_page_count+1]->setItem(i,j,new QTableWidgetItem(comp_two)); if(j==2) SE_Table[order][SE_page_count+1]->setItem(i,j,new QTableWidgetItem(comp_three)); } }
在使用下拉刷新类库EGORefreshTableHeaderView的时候,将资源文件添加到工程中,但是忘记假如QuartzCore.framework框架,造成编译错误:
Undefined symbols for architecture i386:
"_CATransform3DMakeRotation", referenced from:
-[EGORefreshTableHeaderView setState:] in EGORefreshTableHeaderView-6F75314CE1079327.o
"_OBJC_CLASS_$_CALayer", referenced from:
objc-class-ref in EGORefreshTableHeaderView-6F75314CE1079327.o
"_OBJC_CLASS_$_CATransaction", referenced from:
objc-class-ref in EGORefreshTableHeaderView-6F75314CE1079327.o
"_CATransform3DIdentity", referenced from:
-[EGORefreshTableHeaderView setState:] in EGORefreshTableHeaderView-6F75314CE1079327.o
"_kCAGravityResizeAspect", referenced from:
-[EGORefreshTableHeaderView initWithFrame:arrowImageName:textColor:] in EGORefreshTableHeaderView-6F75314CE1079327.o
"_kCATransactionDisableActions", referenced from:
-[EGORefreshTableHeaderView setState:] in EGORefreshTableHeaderView-6F75314CE1079327.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
解决方法:targets——>build phases——>link binary with libraries——>QuartzCore.framework
当添加其他类库的时候也可能因为没有添加framework或者是它们依赖的库同样也会出现这样错误。