看板 C_and_CPP 關於我們 聯絡資訊
開發平台(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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 140.112.175.4 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1668067911.A.6FF.html ※ 編輯: Vvvahc (140.112.175.4 臺灣), 11/10/2022 16:15:59
john790710: 你connect的this(test_show)沒有那個method吧11/10 16:36
john790710: 看錯,是下面不行?change_brigh不要用SLOT包起來試試11/10 16:46
※ 編輯: Vvvahc (140.112.175.4 臺灣), 11/10/2022 16:47:12
Vvvahc: 我不確定有沒有影響,不過我把connect裡的 & show_class 11/10 16:49
Vvvahc: 改回this仍是一樣… 11/10 16:49
Vvvahc: 回j大,那個位置不是得用SLOT包嗎? 11/10 16:54
Vvvahc: 因為前面有single 11/10 16:54
Vvvahc: *SIGNAL 11/10 16:56
john790710: 你用&就不要用SLOT() 11/10 17:14
john790710: https://wiki.qt.io/New_Signal_Slot_Syntax 11/10 17:16
Vvvahc: 將Slot去掉也是沒變化 11/10 17:45
Vvvahc: 我在想是不是得在imshow前連結test_brightness和connect S 11/10 17:45
Vvvahc: lot相關的指標 11/10 17:45
Vvvahc: 不過我在這都一直收到報錯 _ 11/10 17:46
Vvvahc: 更: 我後來加指標int *a去存取SLOT的結果 11/10 18:05
Vvvahc: 經過if判斷,再將Mat img* (*a)便解決了 11/10 18:05
Vvvahc: 謝謝j大 11/10 18:05
wulouise: Qt5可以不用macro了吧 盡量照範例的用 11/10 23:22
wulouise: SIGNAL SLOT runtime很容易就錯過那個error msg..Y 11/10 23:23
lc85301: 可以在 change_brigh 裡面加個 breakpoint 看函式有沒有 11/13 11:15
lc85301: 喔已經解了XD 11/13 11:15
qoorocker: connect 盡量用新語法吧 compile time就知道有沒有成功 11/13 21:58