0
我嘗試更改Gtk :: Window的最小大小。應該可以將窗口縮小到一個更小的尺寸,然後將其中最大的容器縮小到特定的尺寸。Gtkmm - 更改窗口的最小大小
我嘗試了幾種方法,您可以在下面看到。沒有顯示任何效果。最小值始終由圖像大小定義。我做錯了什麼?
的main.cpp
#include "MainWindow.h"
#include <gtkmm.h>
int main (int argc, char* argv[])
{
Glib::RefPtr<Gtk::Application> app =
Gtk::Application::create(argc, argv, "org.gtkmm.example");
MainWindow mainWindow;
return app->run(mainWindow);
}
MainWindow.h
#ifndef MAINWINDOW_H_INCLUDED
#define MAINWINDOW_H_INCLUDED
#include <gtkmm.h>
#include <gdkmm.h>
class MainWindow : public Gtk::Window
{
public:
MainWindow();
private:
Gtk::Image m_Image;
};
#endif // MAINWINDOW_H_INCLUDED
MainWindow.cpp
#include "MainWindow.h"
#define APPROACH_05
MainWindow::MainWindow() :
m_Image("image.png")
{
this->set_border_width(0);
#ifdef APPROACH_01
this->add(m_Image);
m_Image.set_size_request(5,5);
#endif // APPROACH_01
#ifdef APPROACH_02
this->add(m_Image);
this->set_size_request(5,5);
#endif // APPROACH_02
#ifdef APPROACH_03
this->add(m_Image);
Gtk::Allocation allocation = m_Image.get_allocation();
allocation.set_width(5);
allocation.set_height(5);
m_Image.set_allocation(allocation);
#endif // APPROACH_03
#ifdef APPROACH_04
this->add(m_Image);
Gtk::Allocation allocation = this->get_allocation();
allocation.set_width(5);
allocation.set_height(5);
this->set_allocation(allocation);
#endif // APPROACH_04
#ifdef APPROACH_05
this->add(m_Image);
Gdk::Geometry geom = {
.min_width = 5,
.min_height = 5,
};
Gtk::Window::set_geometry_hints(*this,geom,Gdk::HINT_MIN_SIZE);
#endif // APPROACH_05
this->show_all_children();
}
編譯:
g++ main.cpp MainWindow.cpp `pkg-config gtkmm-3.0 --cflags --libs` -o prog
@ptomato感謝您的答覆。我已經試過這種方式:
#ifdef APPROACH_06
this->add(m_ScrolledWindow);
m_ScrolledWindow.set_border_width(0);
m_ScrolledWindow.set_policy(Gtk::POLICY_NEVER,Gtk::POLICY_ALWAYS);
m_ScrolledWindow.add(m_Image);
#endif // APPROACH_06
現在我可以垂直調整窗口的大小,但我看到垂直滾動條。如果像橫軸那樣將策略設置爲POLICY_NEVER
,則窗口寬度限制爲圖像寬度。此外,滑塊的大小也會限制高度。