[問題] Qt使用connect的疑問

作者: Vvvahc (我來搞事了)   2022-11-10 16:11:49
開發平台(Platform): (Ex: Win10, Linux, ...)
win11
編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出)
vs2022
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
Qt、opencv、libtiff
問題(Question):
我在vs中使用Qt分別建立一個QAction和QSlider,該action(open_file)用來開啟tiff-s
tack檔,而QSlider則是控制顯示圖片的亮度。
但我在執行程式時發現slider在滑動時,connect的變數並沒有真正執行與image的變化
想請問是否有其他寫法能順利執行
餵入的資料(Input):
預期的正確結果(Expected Output):
QSlider *myslider可以在cv::imshow更新image時,能及時對next_image做操作
(ex: Mat image*test_brightness) // test_brightness與QSlider連結
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔)
// qt: test_show.cpp
test_show::test_show(QWidget *parent)
:QMainWindow(parent) ,ui(new Ui::testshowclass)
{
connect(open_file,&QAction::triggered,this,&open_and_show:open)
}
//...............
// 從QAction打開文件(16bit *tiff)到imshow都可以順利執行
// 實作libtiff和cv::imshow的class
void open_and_show::open(){
QString Filename = QFileDialog::getopenfilename(this,tr("open"),tr("C:\\));
std::string filename = Filename.toStdString();
const char *mypath = filename.c_str();
TIFF *tif = tiffopen(mypath,"r");
TIFFGetField(tif,TIFFTAG_IMAGELENGTH,&height);
TIFFGetFiled(tif,TIFFTAG_IMAGEWIDTH, &width );
int ntotalframe = TIFFNumberOfDirectories(tif);
uint32_t *slicetif = new uint32_t[width*height];
for(int z=0;z< ntotalframe; ++z){
cv::Mat img(height,width,CV_32FC1,cv::Scalar:all(0));
TIFFReadRGBAImageOriented(tif,width,height,slicetif,ORIENTATION_TOPLEFT);
int ss = 0;
for (int i=0; i<height;i++){
for (int j=0; j<width ;j++){
img.at<float> = TIFFGetR(sliceTif[ss]);
s++;
}
}
TIFFReadDirectory(tif);
int test_brightness = 1;
cv::normalize(img,img,0,65535,cv::NORM_MINMAX,CV_16UC1);
cv::namewindow("hi",cv::WINDOE_GUI_EXPANDED);
cv::imshow("hi",img*test_brightness);
cv::waitkey(10);
}
}
//...............
// 當我想透過QSlider和connect,對img進行操作時卻沒反應
// Qt test_show.cpp
test_show::test_show(QWidget *parent)
:QMainWindow(parent),ui(new UI::testshowclass);{
......
open_and_show *show_class;
connect(myslider,SIGNAL(valueChanged(int)),this,SLOT(&test_show:: change_brigh
));
......
}
void test_show::change_brigh(){
int bri_val = myslider ->value();
show_class.set_brigh(bri_val);
}
//..............
void open_and_show::set_brigh(int brightness) {
if (test_brightness != brightness){
test_brightness = brightness;
}
}
補充說明(Supplement):
test_show是主要的widget
open_and_show則是另個實作的class
作者: john790710 (虎)   2022-11-10 16:36:00
你connect的this(test_show)沒有那個method吧看錯,是下面不行?change_brigh不要用SLOT包起來試試
作者: Vvvahc (我來搞事了)   2022-11-10 16:49:00
我不確定有沒有影響,不過我把connect裡的 & show_class改回this仍是一樣…回j大,那個位置不是得用SLOT包嗎?因為前面有single*SIGNAL
作者: john790710 (虎)   2022-11-10 17:14:00
作者: Vvvahc (我來搞事了)   2022-11-10 17:45:00
將Slot去掉也是沒變化我在想是不是得在imshow前連結test_brightness和connect Slot相關的指標不過我在這都一直收到報錯 _更: 我後來加指標int *a去存取SLOT的結果經過if判斷,再將Mat img* (*a)便解決了謝謝j大
作者: wulouise (在線上!=在電腦前)   2022-11-10 23:22:00
Qt5可以不用macro了吧 盡量照範例的用SIGNAL SLOT runtime很容易就錯過那個error msg..Y
作者: lc85301 (pomelocandy)   2022-11-13 11:15:00
可以在 change_brigh 裡面加個 breakpoint 看函式有沒有喔已經解了XD
作者: qoorocker (rockers)   2022-11-13 21:58:00
connect 盡量用新語法吧 compile time就知道有沒有成功

Links booklink

Contact Us: admin [ a t ] ucptt.com