C#调用Qt写的[email protected]+VS2013(32bit)+win7 32bit

网上有大神教我们怎么用C#调用Qt写的dll,试了一下,没调出来,可能是Qt版本的问题;

今天调了个程序出来,话不多说,写步骤,上代码:

1,从网上下一个qtwinmigrate:

https://github.com/qtproject/qt-solutions/tree/master/qtwinmigrate

发现qtwinmigrate文件夹里面有例程;

2,Qt中编译一下,然后在C#中调用,OK;

3,然后自己写一个程序,编译成dll,用C#调用,OK;

----------------------------------------------------------------

代码如下:

Qt dll:

qtui.pro:

TEMPLATE = lib

CONFIG	    += dll
SOURCES	     = main.cpp \
    dialog.cpp
TARGET	     = qtui
DLLDESTDIR   = $$[QT_INSTALL_PREFIX]/bin
include(../../src/qtwinmigrate.pri)
FORMS += \
    dialog.ui
HEADERS += \
    dialog.h


注意,要把qtwinmigrate文件夹中的src添加到项目中;

头文件:dialog.h

#ifndef DIALOG_H

#define DIALOG_H
#include 
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
    Q_OBJECT
public:
    explicit Dialog(QWidget *parent = 0);
    ~Dialog();
private slots:
    void on_m_pbCalc_clicked();
private:
    Ui::Dialog *ui;
};
#endif // DIALOG_H

cpp:

#include "dialog.h"

#include "ui_dialog.h"
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
}
Dialog::~Dialog()
{
    delete ui;
}
void Dialog::on_m_pbCalc_clicked()
{
    int add1 = ui->m_lE1->text().toInt();
    int add2 = ui->m_lE2->text().toInt();
    int sum = add1+add2;
    QString strSum;
    strSum.setNum(sum);
    ui->m_lbSum->setText(strSum);
    ui->m_lEsum->setText(strSum);
}

main.cpp

#include

#include 
#include 
#include 
#include "dialog.h"
extern "C" __declspec(dllexport) int showDialog()
{
    int argc = 0;
    //char* argv[] = NULL;
    QApplication a(argc, NULL);
    Dialog *dlg = new Dialog();
    dlg->show();
    return a.exec();
}
//int main()
//{
//    Dialog *dlg = new Dialog();
//    dlg->show();
//    return 0;
//}

界面没有啥,就是三个文本框: 实现一个加法;

C#这边简单粗暴:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows;


namespace CSCallQtdll
{
    public partial class Form1 : Form
    {
        [DllImport("qtui.dll")]
        public static extern int showDialog();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            showDialog();
        }
    }
}

界面就一个按钮,把qtui.dll添加进项目就可以了;







你可能感兴趣的:(Qt,Qt,dll,C#)