gpt4 book ai didi

c++ - Gtkmm - 更改窗口的最小尺寸

转载 作者:行者123 更新时间:2023-11-28 02:22:14 28 4
gpt4 key购买 nike

我尝试更改 Gtk::Window 的最小尺寸。应该可以将窗口缩小到比其中最大的容器更小的特定尺寸。

我尝试了几种方法,您可以在下面看到。没有任何效果。最小值始终由图像大小定义。我做错了什么?

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);
}

主窗口.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

主窗口.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,就像在水平轴上一样,窗口宽度将限制为图像宽度。此外, slider 的大小也限制了高度。

最佳答案

如果您希望能够将窗口缩小到比其中的图像更小,那么您需要在将图像添加到 Gtk::ScrolledWindow window 。如果不滚动,当您使窗口小于图像时,图像将不知道要渲染自己的哪一部分。

关于c++ - Gtkmm - 更改窗口的最小尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31990185/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com