test.pro
内容如下:
[C++] 纯文本查看 复制代码 #-------------------------------------------------
#
# Project created by QtCreator 2021-12-30T20:59:13
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test2
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
mainwindow.cpp
HEADERS += mainwindow.h
FORMS += mainwindow.ui
win32: LIBS += -lpsapi
MainWindow.h
[Asm] 纯文本查看 复制代码 #ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:
void on_pushButton_clicked();
void on_pushButton_2_clicked();
void on_comboBox_currentIndexChanged(const QString);
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
main.cpp
[Asm] 纯文本查看 复制代码 #include "mainwindow.h"
#include <QApplication>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QTextCodec *codec = QTextCodec::codecForName("UTF-8"); =======================>加了,编译还是乱码,坑爹啊。
QTextCodec::setCodecForLocale(codec);
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
mainwindows.cpp
[Asm] 纯文本查看 复制代码 #include "mainwindow.h"
#include "ui_mainwindow.h"
#include <windows.h>
#include <psapi.h>
#include <QClipboard>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
DWORD pid;
char buf[MAX_PATH];
HWND h=GetForegroundWindow();
GetWindowThreadProcessId(h,&pid);
HANDLE hProc=OpenProcess(PROCESS_ALL_ACCESS,false,pid);
GetModuleFileNameExA(hProc,(HMODULE)0,buf,sizeof(buf));
CloseHandle(hProc);
ui->lineEdit->setText(buf);
setWindowTitle(buf);
qApp->clipboard()->setText(buf);
}
void MainWindow::on_pushButton_2_clicked()
{
ui->comboBox->addItem("中国");
ui->comboBox->addItem("美国");
ui->comboBox->addItem("日本");
}
void MainWindow::on_comboBox_currentIndexChanged(const QString)
{
//ui->comboBox->currentText();
ui->lineEdit->setText(ui->comboBox->currentText()); //把获取到的comboBox值传给lineEdit
}
|