gpt4 book ai didi

haskell - 表格对每个单元格的点击事件使用react

转载 作者:行者123 更新时间:2023-12-02 03:29:35 24 4
gpt4 key购买 nike

我有一个时间表要显示在表格中。

这是我当前解决方案的示例:

import Graphics.UI.Gtk
import Control.Monad.IO.Class

main = do
initGUI
window <- windowNew
view <- treeViewNew
store <- listStoreNew initialData
treeViewSetModel view store
containerAdd window view
prepareCols view store
window `on` deleteEvent $ liftIO mainQuit >> return False
widgetShowAll window
mainGUI

initialData :: [[String]]
initialData = [["foo", "bar"], ["baz", "42"]]

prepareCols :: TreeView -> ListStore [String] -> IO ()
prepareCols view store = do
size <- listStoreGetSize store
mapM_ (addColumn view store) [0..size-1]

addColumn :: TreeView -> ListStore [String] -> Int -> IO ()
addColumn view store i = addTextColumn view store (!! i) $ show i

addTextColumn :: (TreeViewClass view
, TreeModelClass (model row)
, TypedTreeModelClass model
)
=> view -> model row -> (row -> String) -> String -> IO ()
addTextColumn view model f name = do
col <- treeViewColumnNew
rend <- cellRendererTextNew
treeViewColumnSetTitle col name
treeViewColumnPackStart col rend True
cellLayoutSetAttributes col rend model (\row -> [ cellText := f row ])
treeViewColumnSetExpand col True
treeViewAppendColumn view col
return ()

现在我想让每个单元格都可以右键单击,但 gtk2hs 只提供激活行,而没有激活哪个单元格的信息,或者使列标题可点击。

gtk2hs 中的方法是什么,对于一个响应点击事件的表,其中包含点击了哪一行和哪一列的信息(对于该列,数字索引是完美的,所以我可以使用这个索引来修改我开始使用的列表),而无需诉诸讨厌的事情,例如使用 Table 并在运行时删除/添加标签。

我使用的是 gtk2hs (gtk3) 版本 0.12.5.6

最佳答案

您可以采用中描述的解决方案 this Python answer .

buttonPressEvent 回调可以安装在您的 prepareCols 函数中,用于检查事件是否为鼠标右键单击,然后将事件坐标解码为 TreeStore 路径:

onPathRightClick :: (TreeViewClass view) 
=> view
-> (TreePath -> Int -> IO ())
-> IO (ConnectId view)
onPathRightClick view callback =
on view buttonReleaseEvent $ (return False <*) $ runMaybeT $ do
RightButton <- lift eventButton
(x, y) <- lift eventCoordinates
let x' = round x
y' = round y
(path, col, _cellpoint) <- MaybeT . liftIO $ treeViewGetPathAtPos view (x', y')
colIdx <- MaybeT . liftIO $ findIndex (== col) <$> treeViewGetColumns view
liftIO $ callback path colIdx

prepareCols :: TreeView -> ListStore [String] -> IO ()
prepareCols view store = do
size <- listStoreGetSize store
mapM_ (addColumn view store) [0..size-1]
void $ onPathRightClick view $ \path col -> do
putStrLn . unwords $ [ "Column:" , show col]
putStrLn . unwords $ [ "Path:" , show path]

关于haskell - 表格对每个单元格的点击事件使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912520/

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