[問題] Qt使用connect的疑問

看板C_and_CPP (C/C++)作者 (我來搞事了)時間1年前 (2022/11/10 16:11), 1年前編輯推噓0(0018)
留言18則, 3人參與, 1年前最新討論串1/1
開發平台(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

11/10 16:36, 1年前 , 1F
你connect的this(test_show)沒有那個method吧
11/10 16:36, 1F

11/10 16:46, 1年前 , 2F
看錯,是下面不行?change_brigh不要用SLOT包起來試試
11/10 16:46, 2F
※ 編輯: Vvvahc (140.112.175.4 臺灣), 11/10/2022 16:47:12

11/10 16:49, 1年前 , 3F
我不確定有沒有影響,不過我把connect裡的 & show_class
11/10 16:49, 3F

11/10 16:49, 1年前 , 4F
改回this仍是一樣…
11/10 16:49, 4F

11/10 16:54, 1年前 , 5F
回j大,那個位置不是得用SLOT包嗎?
11/10 16:54, 5F

11/10 16:54, 1年前 , 6F
因為前面有single
11/10 16:54, 6F

11/10 16:56, 1年前 , 7F
*SIGNAL
11/10 16:56, 7F

11/10 17:14, 1年前 , 8F
你用&就不要用SLOT()
11/10 17:14, 8F

11/10 17:16, 1年前 , 9F

11/10 17:45, 1年前 , 10F
將Slot去掉也是沒變化
11/10 17:45, 10F

11/10 17:45, 1年前 , 11F
我在想是不是得在imshow前連結test_brightness和connect S
11/10 17:45, 11F

11/10 17:45, 1年前 , 12F
lot相關的指標
11/10 17:45, 12F

11/10 17:46, 1年前 , 13F
不過我在這都一直收到報錯 _
11/10 17:46, 13F

11/10 18:05, 1年前 , 14F
更: 我後來加指標int *a去存取SLOT的結果
11/10 18:05, 14F

11/10 18:05, 1年前 , 15F
經過if判斷,再將Mat img* (*a)便解決了
11/10 18:05, 15F

11/10 18:05, 1年前 , 16F
謝謝j大
11/10 18:05, 16F

11/10 23:22, 1年前 , 17F
Qt5可以不用macro了吧 盡量照範例的用
11/10 23:22, 17F

11/10 23:23, 1年前 , 18F
SIGNAL SLOT runtime很容易就錯過那個error msg..Y
11/10 23:23, 18F
文章代碼(AID): #1ZRB97R_ (C_and_CPP)
文章代碼(AID): #1ZRB97R_ (C_and_CPP)