28 Temmuz 2021 Çarşamba
21 Haziran 2021 Pazartesi
DETECT USB DEVICE IN QT (QTde usb cihazı eventlarını yakalama)
1) İlk olarak headerlar eklenir
#include <windows.h>
#include <dbt.h>
2)QWidget için virtual fonksiyonu override ederiz herhangi bir usb eventi oluştuğunda artık yakalayabiliriz
bool DT_IFVPLAYER::nativeEvent(const QByteArray& eventType, void* message, long* result){
}
4 Mayıs 2021 Salı
Create Dynamic Library for Windows (Windowsta Dinamik Kütüphane oluşturma)
// DLL olusturuma
1)visual studiodan new Project kısmından Dynamic Library(DLL) seceriz .
2)Fonksiyonları toplayacagımız yeni bir header(.h) dosyası ekleriz
3)Header dosyamızın başına
#pragma once
#define DllImport __declspec( dllimport )
#define DllExport __declspec( dllexport ) ekleriz ve bütün fonksiyonlarımızın basına DllExport ekleriz
4)Headerı istediğimiz yerde include eder fonksiyonları başında DllExport olmadan tanımlarız
5) Build edince .h / .lib ve .dll dosyamızı olusturur alırız
// DLL Kullanma
1)C++->Genel->Include header kısmına header dosyamızın bulunduğu pathı veririz
2)Linker->input->additional dependencies kısmına .lib dosya adını yazarız
3)Linker->General->additional library directories kısmına .lib pathı gösterilir
4).exenin olduğu kısma ise .dlli kopyalamalıyız yoksa hata verecektir
25 Nisan 2021 Pazar
Threadlere özgü alanlar (pthread_key_create fonksiyonu)
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct tagPERSON {
char name[32];
int no;
} PERSON;
void *thread_proc1(void *param);
void *thread_proc2(void *param);
void foo(void);
pthread_key_t g_slotKey;
int main(void)
{
pthread_t tid1, tid2;
int result;
if (pthread_key_create(&g_slotKey, NULL) != 0) {
fprintf(stderr, "pthread_create: %s\n", strerror(result));
exit(EXIT_FAILURE);
}
if ((result = pthread_create(&tid1, NULL, thread_proc1, NULL)) != 0) {
fprintf(stderr, "pthread_create: %s\n", strerror(result));
exit(EXIT_FAILURE);
}
if ((result = pthread_create(&tid2, NULL, thread_proc2, NULL)) != 0) {
fprintf(stderr, "pthread_create: %s\n", strerror(result));
exit(EXIT_FAILURE);
}
pthread_join(tid1, NULL);
pthread_join(tid2, NULL);
pthread_key_delete(g_slotKey);
return 0;
}
void *thread_proc1(void *param)
{
PERSON *per;
if ((per = (PERSON *)malloc(sizeof(PERSON))) == NULL) {
fprintf(stderr, "cannot allocate memory!..\n");
exit(EXIT_FAILURE);
}
strcpy(per->name, "Carol David");
per->no = 333;
pthread_setspecific(g_slotKey, per);
foo();
free(per);
return NULL;
}
void *thread_proc2(void *param)
{
PERSON *per;
if ((per = (PERSON *)malloc(sizeof(PERSON))) == NULL) {
fprintf(stderr, "cannot allocate memory!..\n");
exit(EXIT_FAILURE);
}
strcpy(per->name, "Ferdinand Isaac");
per->no = 765;
pthread_setspecific(g_slotKey, per);
foo();
free(per);
return NULL;
}
void foo(void)
{
PERSON *per;
per = (PERSON *)pthread_getspecific(g_slotKey);
printf("%s, %d\n", per->name, per->no);
}
23 Nisan 2021 Cuma
Unix Linux sistemlerinde Semapor kullanımı
#include <stdio.h>
#include <string.h>
#include<unistd.h>
#include<semaphore.h>
#include<pthread.h>
#include <stdlib.h>
void exit_sys(const char* msg);
void exit_sys_result(const char *msg,int result);
void *thread_proc1(void *param);
void *thread_proc2(void *param);
void do_something(const char *msg);
sem_t g_sem;
int main(void)
{
printf("Hello World!\n");
int result;
pthread_t tid1,tid2;
srand(time(NULL));
if(sem_init(&g_sem,0,1)==-1)
{
exit_sys("sem_init");
}
if((result=pthread_create(&tid1,NULL,thread_proc1,NULL))!=0)
{
exit_sys_result("thread create1",result);
}
if((result=pthread_create(&tid2,NULL,thread_proc2,NULL))!=0)
{
exit_sys_result("thread create2",result);
}
pthread_join(tid1,NULL);
pthread_join(tid2,NULL);
sem_destroy(&g_sem);
return 0;
}
void *thread_proc1(void *param)
{
int i;
for(i=0;i<10;i++)
{
do_something(" thread-1 ");
}
return NULL;
}
void *thread_proc2(void *param)
{
int i;
for(i=0;i<10;i++)
{
do_something(" thread-2 ");
}
return NULL;
}
void do_something(const char *str)
{
sem_wait(&g_sem);
printf("-----------------------\n");
printf("%s: Step-1\n",str);
usleep(rand()%300000);
printf("%s: Step-2\n",str);
usleep(rand()%300000);
printf("%s: Step-3\n",str);
usleep(rand()%300000);
printf("%s: Step-4\n",str);
usleep(rand()%300000);
printf("%s: Step-5\n",str);
usleep(rand()%300000);
sem_post(&g_sem);
}
void exit_sys(const char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}
void exit_sys_result(const char *msg, int result)
{
fprintf(stderr, "%s: %s\n", msg, strerror(result));
exit(EXIT_FAILURE);
}
4 Şubat 2021 Perşembe
QTableview datalarını CSV dosyasına aktarma ( Convert QTableview to CSVFile
void DeviceSettingsClass::tableToCSV()
{
QString filters("CSV files (*.csv);;All files (*.*)");
QString defaultFilter("CSV files (*.csv)");
QString fileName = QFileDialog::getSaveFileName(0, "Save file", QCoreApplication::applicationDirPath(),
filters, &defaultFilter);
QFile file(fileName);
QAbstractItemModel* model = main_class_for_devset->recTableDevice->model();
if (file.open(QFile::WriteOnly | QFile::Truncate)) {
QTextStream data(&file);
QStringList strList;
for (int i = 0; i < model->columnCount(); i++)
{
if (model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString().length() > 0)
strList.append("\"" + model->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString() + "\"");
else
strList.append("");
}
data << strList.join(";") << "\n";
for (int i = 0; i < model->rowCount(); i++)
{
strList.clear();
for (int j = 0; j < model->columnCount(); j++)
{
if (model->data(model->index(i, j)).toString().length() > 0)
strList.append("\"" + model->data(model->index(i, j)).toString() + "\"");
else
strList.append("");
}
data << strList.join(";") + "\n";
}
file.close();
}
}
27 Ocak 2021 Çarşamba
Run Function with Qt::Concurrent in QT (Bir fonksiyonu başka threadde çalıştırma)
//Qt::concurrent ile bir fonksiyonu baska bir threadde calıstırma
örn: Bir void RecordWidget::ProcessGPS_rec2(NvrHandler* hnd, unsigned char* data, unsigned int dataSize) fonksiyonunu başka threadde kosturalım
QFuture<void> future = QtConcurrent::run(this, &RecordWidget::ProcessGPS_rec2, hnd, data,dataSize); şeklinde başka thread tasırız .

