QT 做界面,加载c语言自己编译的动态库

来源:互联网 发布:mac升级失败怎么办 编辑:程序博客网 时间:2024/06/04 18:32

http://blog.csdn.net/huang_jinjin/article/details/7699942

 这两天要用qt做一个界面,但主体功能用要用c语言实现,于是,把c语言的代码生成动态库,在Qt里动态加载该库,拿出来大家分享分享!,

下面是我的QT源码!


main.h文件

1 #ifndef MAIN_WINDOW
  2 #define MAIN_WINDOW
  3 #include<QtGui>
  4 #include<QTextEdit>
  5 
  6 using namespace std;
  7 QString s2q(const string &s);
  8 string q2s(const QString &s);
  9 
 10 class MainWindow : public QMainWindow
 11 {
 12         Q_OBJECT
 13 
 14         public:
 15                 explicit MainWindow(QWidget *parent = 0);
 16                 virtual ~MainWindow();
 17 
 18                 private slots:
 19                         void handleButton();
 20                 void clear_send_box();
 21                 void clear_rev_box();
 22                 void my_show();
 23 
 24         private:
 25                 QWidget *Window;
 26                 QPushButton *send_clear;
 27                 QPushButton *receive_clear;
 28                 QPushButton *send;
 29                 QPushButton *receive;
 30                 QTextEdit *input;
 31                 QTextEdit *output;
 32                 QLabel *lable_send;
 33                 QLabel *lable_rev;
 34                 QString str1;
 35 
 36                 char *str[10];
 37 
 38 
 39 };
 40 #endif

~                                            

main.cpp文件

   #include<QApplication>
  2 #include<QWidget>
  3 #include<QDebug>
  4 #include<QLibrary>
  5 #include"main.h"
  6 
  7 
  8 //说明,到时侯短信放到str 里,需要一个后台的c程序库
  9 
 10 QString s2q(const string &s)
 11 {
 12         return QString(QString::fromLocal8Bit(s.c_str()));
 13 }
 14 
 15 string q2s(const QString &s)
 16 {
 17         return string((const char *)s.toLocal8Bit());
 18 }
 19 
 20 MainWindow::MainWindow(QWidget *parent):QMainWindow(parent)
 21 {
 22         send =new QPushButton("send",this);
 23         send->setGeometry(700, 100, 80, 30);//第一个参数,横坐标的其实
 24 
 25         receive =new QPushButton("receive",this);
 26         receive->setGeometry(700, 300, 80, 30);//第一个参数,横坐标的其实
 27         //位置,第二个总坐标的其实位置,第三个为宽度,第四个为高度
 28 
 29         send_clear =new QPushButton("send_clr",this);
 30         send_clear->setGeometry(700, 200, 80, 30);//第一个参数,横坐标的其实
 31 
 32         receive_clear =new QPushButton("receive_clr",this);
 33         receive_clear->setGeometry(700, 400, 80, 30);//第一个参数,横坐标的其实
 34 
 35         input=new QTextEdit(this);//短信输入对话框
 36         input->resize(200,150);
 37 
 38         output=new QTextEdit(this);//短信输出对话框
 39         output->setGeometry(0, 300, 200, 150);//第一个参数,横坐标的其实
 40 
 41         lable_send =new QLabel("text for send",this);

lable_send->setGeometry(50, 155, 100, 30);//第一个参数,横坐标的其实
 43 
 44         lable_rev =new QLabel("text for receive",this);
 45         lable_rev->setGeometry(50, 455, 110, 30);//第一个参数,横坐标的其实
 46 
 47         connect(send,SIGNAL(clicked()),this,SLOT(handleButton()));
 48         connect(send_clear,SIGNAL(clicked()),this,SLOT(clear_send_box()));
 49         connect(receive_clear,SIGNAL(clicked()),this,SLOT(clear_rev_box()));
 50         connect(receive,SIGNAL(clicked()),this,SLOT(my_show()));
 51 
 52 
 53 
 54 }
 55 MainWindow::~MainWindow()
 56 {
 57 }
 58 
 59 void MainWindow::my_show()
 60 {
 61         QLibrary lib("../C_coder/libprint.so");//库的路径
 62         if(lib.load())
 63         {
 64                 typedef void(*AddFunction)(char *str[]);
 65                 AddFunction Add=(AddFunction)lib.resolve("get_str");//(最后括号里的get_str是写的c函数的名字的名字)()
 66                 if(!Add)
 67                 {
 68 
 69                         qDebug()<<"failed"<<endl;
 70                 }
 71                 else
 72                 {
 73                         Add(str);
 74                         str1= s2q(str[0]);  
 75                         qDebug()<<str[0]<<endl;
 76 
 77                 }
 78                 lib.unload();
 79 

 80         }
 81         else
 82         {
 83 
 84                 qDebug()<<"Failed final"<<endl;
 85         }
 86 }
 87 
 88 void MainWindow::handleButton()
 89 {
 90 
 91 
 92         //QString str1=input->toPlainText();//得到文本框的内容
 93         output->setPlainText(str1);//王文本框里写的
 94         //qDebug()<<str1<<endl;
 95 }
 96 
 97 
 98 void MainWindow::clear_send_box()
 99 {
100 
101         input->setPlainText("");//王文本清空`
102 }
103 
104 void MainWindow::clear_rev_box()
105 {
106 
107         output->setPlainText("");//王文本清空`
108 }
109 
110 int main(int argc,char **argv)
111 {
112         QApplication app(argc ,argv);
113 
114         MainWindow mainwindow;
115         mainwindow.setMinimumSize (1024,700);
116         mainwindow.setWindowTitle("Our Project");
117         mainwindow.show();
118 
119 
120         return app.exec();
                                                                                                                                     120,1-8       97%

}                                                                                                                                     40,0-1        47%
0 0